Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

938. Range Sum of BST #88

Closed
twy30 opened this issue Nov 4, 2020 · 0 comments · Fixed by #90
Closed

938. Range Sum of BST #88

twy30 opened this issue Nov 4, 2020 · 0 comments · Fixed by #90
Labels

Comments

@twy30
Copy link
Contributor

twy30 commented Nov 4, 2020

https://leetcode.com/problems/range-sum-of-bst/

using System;
using System.Collections.Generic;

public class Solution
{
    public int RangeSumBST(TreeNode root, int low, int high)
    {
        // 「根節點」
        var rootNode = root;

        // 「下限值」(含)
        var inclusiveLowerBound = low;

        // 「上限值」(含)
        var inclusiveUpperBound = high;

        // 「節點佇列」
        var nodeQueue = new Queue<TreeNode>();

        // 「輸出值」
        var output = 0;

        nodeQueue.Enqueue(rootNode);

        // 「節點」
        TreeNode node;

        while (nodeQueue.TryDequeue(out node))
        {
            if (node == null) { continue; }

            // 「節點的數值」
            var nodeValue = node.val;

            if (inclusiveLowerBound <= nodeValue && nodeValue <= inclusiveUpperBound)
            {
                output += nodeValue;
            }

            nodeQueue.Enqueue(node.left);
            nodeQueue.Enqueue(node.right);
        }

        return output;
    }
}

參考資料

上限、下限

https://en.wikipedia.org/wiki/Upper_and_lower_bounds

  • 上限: upper bound 或 majorant
  • 下限: lower bound 或 minorant

包含、不含


請參考「刷 LeetCode 練習命名」 #69 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

1 participant