树和二叉树
2015-06-27 11:55
239 查看
树和二叉树
树和二叉树同样的树
倒转二叉排序树
求二叉树的深度
求二叉树的最小深度
路径和
LCALowest Common Ancestor
同样的树
判断两棵树的结构和值是否全部一样# Definition for a binary tree node. # class TreeNode # attr_accessor :val, :left, :right # def initialize(val) # @val = val # @left, @right = nil, nil # end # end # 以上是二叉树的ruby声明,下面凡是使用ruby编写的解法都用此声明 # @param {TreeNode} p # @param {TreeNode} q # @return {Boolean} def is_same_tree(p, q) return true if p.nil? && q.nil? return false if p.nil? || q.nil? || p.val != q.val return is_same_tree(p.left, q.left) && is_same_tree(p.right, q.right) end
Same Tree
倒转二叉排序树
其实就是翻转左右子树,递归版的写起来很简单# Ruby def invert_tree(root) return root if root == nil root.left, root.right = root.right, root.left invert_tree(root.left) invert_tree(root.right) root end
非递归版的用两个队列做层序遍历, 逻辑也很简单
def invert_tree(root) return root if root == nil arr1, arr2 = [root], [] until arr1.empty? do arr2 << arr1.shift until arr1.empty? arr2.each do |r| r.left, r.right = r.right, r.left arr1 << r.left if r.left arr1 << r.right if r.right end arr2.clear end root end
心情好,又写了个c++版的非递归
/** * 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: TreeNode* invertTree(TreeNode* root) { if(root == NULL) return root; TreeNode *tmp, *it; queue<TreeNode*> q1, q2; q1.push(root); while(!q1.empty()){ while(!q1.empty()){ it = q1.front(); q2.push(it); q1.pop(); } while(!q2.empty()){ it = q2.front(); q2.pop(); if(it == NULL) continue; tmp = it->left; it->left = it->right; it->right = tmp; q1.push(it->left); q1.push(it->right); } } return root; } };
Invert Binary Tree
求二叉树的深度
用层序遍历求二叉树的深度,方法几乎和上例一样# Ruby def max_depth(root) return 0 unless root arr1 = [root] depth = 0 arr2 = [] until arr1.empty? do depth += 1 arr2 << arr1.shift until arr1.empty? until arr2.empty? do tmp = arr2.pop arr1 << tmp.left if tmp.left arr1 << tmp.right if tmp.right end end depth end
Maximum Depth of Binary Tree
求二叉树的最小深度
二叉树的最小深度即从根节点到最近的叶子节点的路径长度。非递归层序遍历(双栈)解法:
# python def minDepth(self, root): stack1 = [root] stack2 = [] mindepth = 0 while stack1: if stack1[0]: mindepth += 1 while stack1: stack2.append(stack1.pop()) while stack2: node = stack2.pop() if node: if node.left: stack1.append(node.left) if node.right: stack1.append(node.right) if not node.left and not node.right: return mindepth return mindepth
我的递归解法:
def minDepth(self, root): if not root: return 0 if not root.left and not root.right: return 1 if root.left and root.right: return 1 + min(self.minDepth(root.left), self.minDepth(root.right)) if root.right: return self.minDepth(root.right) + 1 else: return self.minDepth(root.left) + 1
是不是很啰嗦,leetcode上最近有个很活跃的牛人StefanPochmann (49,810 points) , 多项魔方世界纪录保持者,能用5个球玩Juggling,各种问题都喜欢用1~3行的代码搞定,下面是他的递归解法,是不是很简练?
def minDepth(self, root): if not root: return 0 a, b = sorted(map(self.minDepth, (root.left, root.right))) return 1 + (a or b)
Minimum Depth of Binary Tree
路径和
判断从根到叶子的路径和是否与给定的数相同For example:
Given the below binary tree and sum = 22,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
当存在根到叶子的路径 5->4->11->2 的和是22,返回true
/* C */ bool hasPathSum(struct TreeNode* root, int sum) { if(!root) return false; if(root->val == sum && !root->left && !root->right) return true; hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
我只能说,太适合用递归的先序遍历了
LCA(Lowest Common Ancestor)
def lowest_common_ancestor(root, p, q) return root if root.nil? || root == p || root == q left = lowest_common_ancestor root.left, p, q right = lowest_common_ancestor root.right, p, q return root if left && right left or right end
相关文章推荐
- javascript call 详细解释
- paypal 错误处理:This transaction couldn\'t be completed. Please redirect your customer to PayPal.
- MSP430入门-----总算读懂例程中的用调节DCO频率
- 公司产品
- 动态顺序表(可分配内存空间)
- openerp如何禁用单击后弹窗,以及使tree view可编辑
- js实现省市选择二级联动
- 洪晃说
- 项目笔记:1.nginx的七层的负载均衡集群
- 菜鸟的mongoDB学习---(五)MongoDB的limit、skip、sort方法
- 欢迎使用CSDN-markdown编辑器
- Java中ArrayList和LinkedList区别
- CMake快速入门
- CodeForces 550C Divisibility by Eight(枚举)
- Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>
- iOS_小问题积累(自定义tableviewcell时覆盖父类的变量出现的问题)
- iOS应用开发:什么是ARC?
- 解决默认安装AndroidX86 2.2以上版本不能上网
- easyui datagrid显示进度条控制操作
- 数据结构推荐的书籍