Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the right pointer in TreeNode as the nextpointer in ListNode. Notice ...
分类:
其他好文 时间:
2017-10-13 10:28:21
阅读次数:
160
class Solution { public List preorderTraversal(TreeNode root) { Stack stack=new Stack(); List res=new ArrayList(); while(root!=null||!stack.isEmpty())... ...
分类:
其他好文 时间:
2017-10-12 10:08:42
阅读次数:
181
二叉树遍历最简单的就是递归了。因为递归实质上是栈存了一些中间值,所以我们可以使用stack实现迭代版的遍历。 中序遍历 步骤: 首先将root节点作为当前节点。 1.如果当前节点不为空,压入当前节点。将左节点作为当前节点。 2.否则弹出栈顶节点作为当前节点,输出当前节点。 3.如果右节点不为空,右节 ...
分类:
其他好文 时间:
2017-10-11 21:48:46
阅读次数:
183
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 方法一:迭代 方法二:递归 ...
分类:
其他好文 时间:
2017-10-10 19:20:19
阅读次数:
147
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to ou ...
分类:
其他好文 时间:
2017-10-09 13:11:11
阅读次数:
153
Given a binary tree, return all root-to-leaf paths. Example Given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [ "1->2->5", "1 ...
分类:
其他好文 时间:
2017-10-08 15:35:21
阅读次数:
158
第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~ 1 /* inorder traversal binary tree */ 2 #include 3 #include 4 5 6 struct TreeNode { 7 int val; 8 struct... ...
分类:
其他好文 时间:
2017-10-01 14:25:12
阅读次数:
159
public class Solution { public List inorderTraversal(TreeNode root) { List res=new ArrayList(); Stack stack=new Stack(); while(root!=null||!stack.isEm... ...
分类:
其他好文 时间:
2017-09-29 09:56:30
阅读次数:
116
Given A binary Tree, how do you remove all the half nodes (which has only one child)? Note leaves should not be touched as they have both children as ...
分类:
其他好文 时间:
2017-09-29 09:52:12
阅读次数:
156
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 解题思路:最经典的递归,已 ...
分类:
其他好文 时间:
2017-09-28 00:31:25
阅读次数:
134