# Trees topic # 98. Validate Binary Search Tree ## Question: (Medium) Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: * The left subtree of a node contains only nodes with keys less than the node's key. * The right subtree of a node contains only nodes with keys greater than the node's key. * Both the left and right subtrees must also be binary search trees. ## Solution: (C++) ```c= // 利用inorder traversal來解 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> record; void inorderTraversal(TreeNode* root){ if(root==NULL) return; inorderTraversal(root->left); record.push_back(root->val); inorderTraversal(root->right); } bool isValidBST(TreeNode* root) { inorderTraversal(root); for(int i=1; i<record.size(); i++){ if(record[i]<=record[i-1]) return false; } return true; } }; ``` ```c= // using recursive class Solution { public: bool check_node(TreeNode* root, long long int left, long long int right){ if(root==NULL) return true; if(root->val>left && root->val<right){ return check_node(root->right, root->val, right) && check_node(root->left, left, root->val); } else{ return false; } } bool isValidBST(TreeNode* root) { long long int maxima = 12147483648; long long int minima = -12147483648; cout<<minima<<" "<<maxima<<endl; if(root->left == NULL && root->right == NULL) return true; else return check_node(root, minima, maxima); } }; ``` ## Notes: 1. 個人覺得用inorder來解比較直觀,因為inorder的output必定是由小到大,所以只需要檢查順序即可 2. recursive則是寫一個function,判斷該node是否符合條件,有沒有大於left,有沒有小於right --- --- # 2265. Count Nodes Equal to Average of Subtree ## Question (Medium) Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree. Note: The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer. A subtree of root is a tree consisting of root and all of its descendants. ![image.png](https://hackmd.io/_uploads/B1UF39xmp.png) ## Solution (C++) ```c= /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int ans = 0; void post_order(TreeNode* node, int& sum, int& count){ if(node == NULL){ return ; } post_order(node->left, sum, count); int temp = count; int temp_sum = sum; sum-=temp_sum; count-=temp; post_order(node->right, sum, count); count+=temp; sum+=temp_sum; count++; sum+=node->val; if(node->val == floor(double(sum/count))){ ans++; } } int averageOfSubtree(TreeNode* root) { int sum = 0, count = 0; post_order(root, sum, count); return ans; } }; ``` ## Notes: * 這題用postorder先算出left subtree的總和和次數,再計算right subtree的總和和次數 * 比較麻煩的點是在遍歷right subtree不需要考慮left subtree的總和和node數量。 * 所以需要做一點改變(line 20~24),我們在遍歷完left subtree時,先把summation的值減掉,遍歷完right subtree的時候,再把它加回來。 --- ## 103. Binary Tree Zigzag Level Order Traversal ```cpp= /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { queue<pair<TreeNode*, int>> q; vector<vector<int>> ans; q.push(make_pair(root, 0)); while(q.size()!=0){ int qs = q.size(); vector<int> temp_arr; for(int i=0; i<qs; i++){ pair<TreeNode*, int> temp = q.front(); q.pop(); if(temp.first == NULL) continue; if(temp.second%2==0) temp_arr.push_back(temp.first->val); else temp_arr.insert(temp_arr.begin(), temp.first->val); q.push(make_pair(temp.first->left, temp.second+1)); q.push(make_pair(temp.first->right, temp.second+1)); } if(temp_arr.size()>0) ans.push_back(temp_arr); } return ans; } }; ``` ### Notes: 1. level order traversal 2. 其實就是bfs,只是要注意level,如果是奇數層則要反過來traverse。