The depth of binary tree and the judgement of two-fork balance tree

Source: Internet
Author: User

My code: The idea of using the previous one to find a path of a binary tree and a specific valueSource Code
 
   
  
  1. struct TreeNode{
  2. int val;
  3. struct TreeNode *left;
  4. struct TreeNode *right;
  5. };
  
 
  1. #ifndef BINARY_TREE_DEEP_H
  2. #define BINARY_TREE_DEEP_H
  3. #include "reconstructBinaryTree.h"
  4. #include<stack>
  5. #include<set>
  6. void treeDeepCore(TreeNode *node ,std::stack<TreeNode*> &v_stack,std::set<int> &v_set);
  7. int getBinaryTreeDeep(TreeNode **root){
  8. if(root==NULL||*root==NULL){
  9. return 0;
  10. }
  11. std::stack<TreeNode*> g_stack;
  12. std::set<int> g_set;
  13. treeDeepCore(*root,g_stack,g_set);
  14. std::cout<<*(g_set.begin())<<std::endl;
  15. return *(g_set.rbegin());
  16. }
  17. void treeDeepCore(TreeNode *node ,std::stack<TreeNode*> &v_stack,std::set<int> &v_set){
  18. if(node==NULL){
  19. return;
  20. }
  21. v_stack.push(node);
  22. if(node->left==NULL&&node->right==NULL){
  23. v_set.insert(v_stack.size());
  24. }
  25. treeDeepCore(node->left,v_stack,v_set);
  26. treeDeepCore(node->right,v_stack,v_set);
  27. if(!v_stack.empty()){
  28. v_stack.pop();
  29. }
  30. }
  31. #endif
Test Code
  
 
  1. #include"binaryTreeDeep.h"
  2. int main(){
  3. int pre[8]={1,2,4,7,3,5,6,8};
  4. int mid[8]={4,7,2,1,5,3,8,6};
  5. struct TreeNode *root=reconstructBinaryTree(pre,mid,8); //重建二叉树
  6. std::cout<<getBinaryTreeDeep(&root);
  7. }

For example, in order to find the depth of a tree we can actually do this, nothing but to find the length of the maximum path, with the idea of recursion can solve the problem. You see the maximum depth is the following cases: Left dial hand tree is empty, the right subtree is not empty: depth is Zuozi depth plus 1. The left subtree is not empty, the right subtree is empty: depth is the depth of the right subtree plus 1 so the subtree is not empty: the depth is the maximum depth of the two is 1.

  
 
  1. int binaryTreeDeepCore(TreeNode *node){
  2. if(node==NULL){
  3. return 0;
  4. }
  5. int leftDeep=binaryTreeDeepCore(node->left);
  6. int rightDeep=binaryTreeDeepCore(node->right);
  7. return (leftDeep>rightDeep)? (leftDeep+1):(rightDeep+1);
  8. }
On this basis to determine whether a tree is a balanced binary tree, so know, that is, to see if a tree exists two or so node depth difference greater than 1 well.Source Code
  
 
  1. #ifndef IS_BALANCETREE_H
  2. #define IS_BALANCETREE_H
  3. #include"reconstructBinaryTree.h"
  4. int binaryTreeDeepCore(TreeNode *node);
  5. bool isBalanceBT(TreeNode *root){
  6. if(root==NULL){
  7. return true;
  8. }
  9. int leftDeep=binaryTreeDeepCore(root->left);
  10. int rightDeep=binaryTreeDeepCore(root->right);
  11. int diffDeep=leftDeep-rightDeep;
  12. if(diffDeep>1||diffDeep<-1)
  13. return false;
  14. return binaryTreeDeepCore(root->left)&&binaryTreeDeepCore(root->right);
  15. }
  16. int binaryTreeDeepCore(TreeNode *node){
  17. if(node==NULL){
  18. return 0;
  19. }
  20. int leftDeep=binaryTreeDeepCore(node->left);
  21. int rightDeep=binaryTreeDeepCore(node->right);
  22. return (leftDeep>rightDeep)? (leftDeep+1):(rightDeep+1);
  23. }
  24. #endif




From for notes (Wiz)

The depth of binary tree and the judgement of two-fork balance tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.