222. Count Complete Tree Nodes

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int countNodes(TreeNode root) {

        if(root==null)
            return 0;
        //获取到左右两个深度
        int leftDepth=getLeftDepth(root)+1;
        int rightDepth=getRightDepth(root)+1;
        if(leftDepth==rightDepth)
        {

            //这里采用位运算可以让实际那复杂度更加低,如果调用pow计算的话,会超时
            return (2<<(leftDepth-1)) - 1;
            //return (int)Math.pow(2,leftDepth)-1;
        }
        else
        {
            return countNodes(root.left)+countNodes(root.right)+1;
        }

    }

    public int getLeftDepth(TreeNode root)
    {
        int res=0;
        TreeNode temp=root.left;
        while(temp!=null)
        {
            res++;
            temp=temp.left;

        }
        return res;
    }

    public int getRightDepth(TreeNode root)
    {
        int res=0;
        TreeNode temp=root.right;
        while(temp!=null)
        {
            res++;
            temp=temp.right;
        }
        return res;

    }

}
时间: 2024-11-06 08:29:21

222. Count Complete Tree Nodes的相关文章

8.8 LeetCode 222 Count Complete Tree Nodes

Question: Count Complete Tree Nodes Total Accepted: 11040 Total Submissions: 53992My Submissions Question Solution Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree

【LeetCode】222. Count Complete Tree Nodes

Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last le

[LeetCode] 222. Count Complete Tree Nodes Java

题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as

[leedcode 222] Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos

[Leetcode] Binary search -- 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos

LeetCode OJ 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos

Java for LeetCode 222 Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as po

(medium)LeetCode 222.Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos

LeetCode 222. Count Complete Tree Nodes

complete binary tree:除最后一行外每一行的节点都有两个儿子,最后一行的节点尽可能靠左. ver0: 1 class Solution { 2 public: 3 int countNodes(TreeNode* root) { 4 if(!root) return 0; 5 return 1 + countNodes(root->left) + countNodes(root->right); 6 } 7 }; 不出意料地TLE. ver1: 1 class Solutio