address
https://www.acwing.com/problem/content/66/
https://www.acwing.com/problem/content/67/
https://www.acwing.com/problem/content/submission/68/
Three questions are related to the use of binary tree recursive traversal can be solved
70. The k-th node of the binary search tree
Given a binary search tree, please find the first k smaller nodes therein.
You may assume that tree and k are present, and 1≤k≤ tree summary points.
Entry
Input: the root = [ 2 , . 1 , 3 , null , null , null , null ], K = 3 2 / \ . 1 3 Output: 3
Inorder traversal count additionally added K
When traversing the K nodes to find the target node
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* ans; 13 TreeNode* travel(TreeNode* root, int& k) 14 { 15 if(root == NULL) return NULL; 16 17 travel(root->left,k); 18 k--; 19 if(k ==0) ans = root; 20 21 travel(root->right,k); 22 23 24 return NULL; 25 } 26 27 TreeNode* kthNode(TreeNode* root, int k) { 28 travel(root,k); 29 30 return ans; 31 } 32 };
71. The binary tree of depth
Input binary tree root, the depth of the tree request.
Forming a path tree from the root node to the leaf node sequentially passes (including the root, leaf nodes), the depth of the length of the longest path in the tree.
Entry
Input: binary tree [ 8 , 12 is , 2 , null , null , . 6 , . 4 , null , null , null , null ] as shown below: 8 / \ 12 is 2 / \ . 6 . 4 Output: 3
Adding layers to traverse recursive traversal
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int ans =0; 13 14 void travel(TreeNode* root,int k){ 15 if(root == NULL){ 16 if(k > ans) 17 ans =k; 18 return; 19 } 20 travel(root->right,k+1); 21 travel(root->left,k+1); 22 23 } 24 25 int treeDepth(TreeNode* root) { 26 if(root == NULL) return 0; 27 travel(root,0); 28 29 30 return ans; 31 } 32 };
72. The balanced binary tree
Input binary tree root node, the tree determination is not a balanced binary tree.
If the depth of the left and right subtrees of any node of a binary tree differs by no more than one point, then it is a balanced binary tree.
note:
- Provisions empty tree is a balanced binary tree.
Entry
Input: binary [ 5 , . 7 , . 11 , null , null , 12 is , . 9 , null , null , null , null ] as shown below, 5 / \ . 7 . 11 / \ 12 is . 9 Output: to true
Recording depth recursive traversal of each node in the subtree back up that a large depth of about sub-trees
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool ans =true; 13 int isBalancedInner(TreeNode* root,int k) 14 { 15 if(root == NULL) return k; 16 17 int l = isBalancedInner(root->left,k+1); 18 int r = isBalancedInner(root->right,k+1); 19 20 if(abs(l-r) > 1) ans =false; 21 22 return max(l,r); 23 } 24 25 bool isBalanced(TreeNode* root) { 26 isBalancedInner(root,0); 27 28 return ans; 29 } 30 };