
Tree
pengweixuan008
这个作者很懒,什么都没留下…
展开
-
226.Invert Binary Tree(交换二叉树左右结点)
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1方法:先创建一个空结点,将左右结点交换,再用递归代码:/** * Definition for a binary原创 2015-07-28 14:36:43 · 304 阅读 · 0 评论 -
100.Same Tree (判断两颗树是否相等)
public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null&&q==null){ return true; }else if(p==null||q==null){ return false原创 2015-08-03 15:17:09 · 406 阅读 · 0 评论 -
104.Maximum Depth of Binary Tree(二叉树深度)
public class Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; if(root.left==null&&root.right==null) return 1; int x = Math.max(maxDepth(root.l原创 2015-08-03 15:05:22 · 268 阅读 · 0 评论 -
108.Convert Sorted Array to Binary Search Tree (将有序数组转化成二叉排序树)
public class Solution { public TreeNode sortedArrayToBST(int[] nums) { return build(nums,0,nums.length-1); } public TreeNode build(int[]nums,int left,int right){原创 2015-08-03 15:01:38 · 349 阅读 · 0 评论 -
145.Binary Tree Postorder Traversal (二叉树后序遍历)
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].代码:/** * Defini原创 2015-07-30 14:43:37 · 258 阅读 · 0 评论 -
144.Binary Tree Preorder Traversal(二叉树先序遍历)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Sol原创 2015-07-30 15:00:30 · 239 阅读 · 0 评论 -
116.Populating Next Right Pointers in Each Node(二叉完全树的next结点)
Note:You may only use constant extra space.You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).For example,Given the原创 2015-07-30 15:17:48 · 209 阅读 · 0 评论 -
112.Path Sum (数的路径为N)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2015-08-03 14:54:13 · 348 阅读 · 0 评论 -
二叉搜索树与双向链表转化
1:由于要求链表是有序的,可以借助二叉树中序遍历,因为中序遍历算法的特点就是从小到大访问结点。当遍历访问到根结点时,假设根结点的左侧已经处理好,只需将根结点与上次访问的最近结点(左子树中最大值结点)的指针连接好即可。进而更新当前链表的最后一个结点指针。2:由于中序遍历过程正好是转换成链表的过程,即可采用递归处理struct BinaryTreeNode {转载 2015-08-09 16:46:30 · 372 阅读 · 0 评论 -
94.Binary Tree Inorder Traversal(二叉树先序遍历)
For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].public class Solution { public List inorderTraversal(TreeNode root) { List res = new原创 2015-08-03 15:25:56 · 235 阅读 · 0 评论