Sword finger offer--040-Balanced binary tree (judging whether a binary tree is a balanced binary tree) [extended additional question]

Source: Internet
Author: User

Link

New Ket OJ: Balanced binary tree

Nine degrees OJ: not included

GitHub code: 040-Balanced binary tree

CSDN: Sword offer–040-Balanced binary tree

New Ket OJ Nine degrees OJ csdn GitHub Code
040-balanced binary tree Not included Sword Finger offer–040-Balanced two-fork tree 040-balanced binary tree
Test instructions

Title Description

Enter a binary tree to determine whether the two-prong tree is a balanced binary tree.

Recursive method

According to the definition of the balanced binary tree

Balanced binary tree requirements for each node, its left and right sub-tree height difference cannot exceed 1

So we judge each root node recursively, judging the height difference of the left and right subtree.

Recursion gets the depth of the two fork tree

First get the depth of the two fork tree

int TreeDepth(TreeNode *root){    if(root == NULL)    {    return0;    }    int leftDepth = TreeDepth(root->left);    int rightDepth = TreeDepth(root->right);    //  返回左右子树中深度最深的    return1;}

Below we recursively determine whether the left and right subtree of each node satisfies the equilibrium condition.

bool IsBalanced_Solution(TreeNode* root){    if(root == NULL)    {        returntrue;    }    int leftDepth = TreeDepth(root->left);    int rightDepth = TreeDepth(root->right);    if(fabs1)    {        return IsBalanced_Solution(root->left) && IsBalanced_Solution(root->right);    }    else    {        returnfalse;    }}
Full code

The complete code is as follows

#include <iostream>#include <cmath>using namespace STD;//DEBUG Switch#define __tmain Main#ifdef __tmain#define DEBUG Cout#else#define DEBUG 0 && cout#endif //__tmain #ifdef __tmainstructtreenode{intValstructTreeNode *left;structTreeNode *right;};#endif //__tmain classsolution{ Public:BOOLIsbalanced_solution (treenode* root) {if(Root = NULL) {return true; }intLeftdepth = Treedepth (root->left);intRightdepth = Treedepth (root->right);if(fabs(leftdepth-rightdepth) <=1)        {returnIsbalanced_solution (Root->left) && isbalanced_solution (root->right); }Else{return false; }    }intTreedepth (TreeNode *root) {if(Root = NULL) {return 0; }intLeftdepth = Treedepth (root->left);intRightdepth = Treedepth (root->right);//returns the deepest depth in the left and right subtree        returnMax (leftdepth, rightdepth) +1; }};int__tmain () {//0//1 2//3TreeNode tree[4]; tree[0].val =0; tree[0].left = &tree[1]; tree[0].right = &tree[2]; tree[1].val =1; tree[1].left = &tree[3]; tree[1].right = NULL; tree[2].val =2; tree[2].left = NULL; tree[2].right = NULL; tree[3].val =3; tree[3].left = NULL; tree[3].right = NULL; Solution Solu;cout<<solu. Isbalanced_solution (tree) <<endl;return 0;}

But this recursive method has very big flaw, in seeking the node's left and right sub-tree depth to traverse through the tree, again to judge the balance of the subtree and then traverse through the tree structure, resulting in traversal multiple times.

Is there a way to determine if each node is balanced while traversing the tree.

Recursive improvement (side traversal edge judgment)

We use depth to preserve the number of layers in a recursive process, and then iterate through the process synchronously

classsolution{ Public:balance in synchronous judgment ////in recursive process    BOOLIsvalwithdepth (TreeNode *root,int*depth) {if(Root = NULL) {*depth =0;return true; }intLeftdepth, rightdepth;BOOLleft = Isvalwithdepth (Root->left, &leftdepth);BOOLright = Isvalwithdepth (Root->right, &rightdepth);if(left = =true&& right = =true)        {if(fabs(leftdepth-rightdepth) <=1) {*depth = max (leftdepth, rightdepth) +1; Debug <<"depth ="<<*depth <<endl;return true; }        }return false; }};

Sword finger offer--040-Balanced binary tree (determines if a binary tree is a balanced binary tree) [extended additional question]

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.