Count Complete Tree Nodes
来源:互联网 发布:华硕路由器端口转发 编辑:程序博客网 时间:2024/05/22 16:41
/** * 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: void traverseLeft(TreeNode* root, int& h) { if(root == NULL) { return; } ++ h; traverseLeft(root -> left, h); } void traverseRight(TreeNode* root, int& h) { if(root == NULL) { return; } ++ h; traverseRight(root -> right, h); } int countNodes(TreeNode* root) { if(root == NULL) { return 0; } int h1 = 1; int h2 = 1; traverseLeft(root -> left, h1); traverseRight(root -> right, h2); if(h1 == h2) { return pow(2, h1) - 1; } //int h = max(h1, h2); return countNodes(root -> left) + countNodes(root -> right) + 1; }};
0 0
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes(****)
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- iOS调用系统声音和振动
- Java设计模式之责任链模式
- jbtoyjmqme
- leetcode--Binary Tree Maximum Path Sum
- SSL库比较
- Count Complete Tree Nodes
- LeetCode20:Valid Parentheses
- 每个程序员应知晓的编译器优化相关内容(第 2 部分)
- Block的链式编程
- msl、ttl及rtt的区别—— TCP控制字段标志
- 2015.07.10总结
- 解决前台向mysql更新数据的编码问题
- JS返回上一步
- ASP.NET按钮点击后弹出确认提示