[Prove safety -Offer] 55 -. I. II binary tree depth and a balanced binary tree (binary, postorder traversal, code optimization, ingenious solution)

1. Source title

Links: depth I. binary tree
link: II a balanced binary tree.
Source: LeetCode-- "prove safety -Offer" special

2. Description title

Here Insert Picture Description
Here Insert Picture Description

3. title parsing - binary tree of depth I.

Method a: Method for Recursive routine +

Starting from the root traversal:

  • If only the root node, the depth of 1
  • If only the left sub-tree root node is not the right subtree, then the depth of the left subtree depth +1
  • If the root is not only the right subtree the left subtree, then the depth of the left subtree depth +1
  • If the left and right subtrees are present, a large value of the depth of the depth of the tree of its left subtree, then right subtree +1

This recursive ideas can be realized using.

See the code below:

// 执行用时 :16 ms, 在所有 C++ 提交中击败了46.43%的用户
// 内存消耗 :21.4 MB, 在所有 C++ 提交中击败了100.00%的用户

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        return (left > right) ? (left + 1) : (right + 1);
    }
};

4. Analytical Title -. II balanced binary tree

Method a: Method for Recursive routine +

And then seek to solve this problem with the depth of experience after the binary tree, we can easily think of an idea:

  • When traversing each node of the tree, function calls maxDepthto obtain the depth of its left and right subtrees
  • If the depth of each junction point of the left and right subtrees not differ by more than one, by definition, it is a balanced binary tree.

See the code below:

// 执行用时 :16 ms, 在所有 C++ 提交中击败了79.85%的用户
// 内存消耗 :22.7 MB, 在所有 C++ 提交中击败了100.00%的用户

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if (root == nullptr) return true;
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        int diff = left - right;
        if (diff > 1 || diff < -1) return false;

        return isBalanced(root->left) and isBalanced(root->right);
    }

    int maxDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        return (left > right) ? (left + 1) : (right + 1);
    }
};

Method Two: recursion optimization ingenious solution +

To the above method, the code is simple of course, but we also note that recursion makes a traversed node will be repeated many times, this line of thinking time efficiency is not high.
Here Insert Picture Description
Here Insert Picture Description
Postorder: When using the traversing, prior to traversing a node, has traversed its left and right subtrees, as long as the recording its depth when traversing each node (a node is equal to the depth of it to the leaf length of the path node), while we can traverse while determining each node is not balanced.

// 执行用时 :12 ms, 在所有 C++ 提交中击败了93.83%的用户
// 内存消耗 :23.7 MB, 在所有 C++ 提交中击败了100.00%的用户

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        int depth = 0;
        return help(root, &depth);
    }

    bool help(TreeNode* root, int* pDepth) {
        if (root == nullptr) {
            *pDepth = 0;
            return true;
        }
        int left;
        int right;
        if (help(root->left, &left) and help(root->right, &right)) {
            int diff = left - right;
            if (diff <= 1 and diff >= -1) {
                *pDepth = 1 + (left > right ? left : right);
                return true;
            }
        }
        return false;
    }
};
Published 343 original articles · won 197 Like · views 60000 +

Guess you like

Origin blog.csdn.net/yl_puyu/article/details/104756110