随笔分类 -  Tree

摘要:recursive (O(logn) space) and iterative solutions (O(1) space) This is not a very intuitive problem for me, I have to spent quite a while drawing figu 阅读全文
posted @ 2017-12-02 10:14 apanda009 阅读(185) 评论(0) 推荐(0)
摘要:How do we solve problem like this if we were given a normal tree? Yes, traverse it, keep a root to leaf running sum. If we see a leaf node (node.left 阅读全文
posted @ 2017-11-17 03:35 apanda009 阅读(260) 评论(0) 推荐(0)
摘要:找规律? 我们通过举一些正确的例子,比如"9,3,4,#,#,1,#,#,2,#,6,#,#" 或者"9,3,4,#,#,1,#,#,2,#,6,#,#"等等,可以观察出如下两个规律: 1. 数字的个数总是比#号少一个 2. 最后一个一定是#号 那么我们加入先不考虑最后一个#号,那么此时数字和#号的 阅读全文
posted @ 2017-08-07 18:40 apanda009 阅读(161) 评论(0) 推荐(0)
摘要:在Binary Tree Level Order Traversal中我们是维护了一个队列来完成遍历,而在这里为了使每次都倒序出来,我们很容易想到用栈的结构来完成这个操作。有一个区别是这里我们需要一层一层的来处理(原来可以按队列插入就可以,因为后进来的元素不会先处理),所以会同时维护新旧两个栈,一个 阅读全文
posted @ 2017-08-07 00:07 apanda009 阅读(179) 评论(0) 推荐(0)
摘要:递归的题分为考察: 分治? 先序? 中序? 先序输入值? 全局辅助值? 全局结果值? 分治法(后序遍历), 先序输入值, 后序结果值 + 叶结点(叶结点也要操作 sum * 10 类似 513. Find Bottom Left Tree Value中的deep ) 这是一道树的题目,一般使用递归来 阅读全文
posted @ 2017-08-06 21:22 apanda009 阅读(119) 评论(0) 推荐(0)
摘要:现在开始考bfs 了吗, 根据size办事,分情况了 dfs: 先序遍历 + 树的深度, 跟此题类似: 199 Binary Tree Right Side View 阅读全文
posted @ 2017-08-06 18:08 apanda009 阅读(165) 评论(0) 推荐(0)
摘要:经典bfs, 不多说 阅读全文
posted @ 2017-08-06 17:39 apanda009 阅读(148) 评论(0) 推荐(0)
摘要:L: mirror tree: Traverse both left and right branches of the root symmetricaly and check if the values are equal. 想testcase, 如何遍历, 与谁比较, 是否为空, 就是判断值和关 阅读全文
posted @ 2017-08-03 19:35 apanda009 阅读(132) 评论(0) 推荐(0)
摘要:到叶子所以加(left == null && right == null && 当前值满足条件) 后序遍历 The basic idea is to subtract the value of current node from sum until it reaches a leaf node an 阅读全文
posted @ 2017-08-02 12:38 apanda009 阅读(132) 评论(0) 推荐(0)
摘要:递归后序遍历, 因为有返回值, 所以要后序遍历, 在递归回溯后返的时候进行操作: 一般在改变递归函数的输入值的时候加上 这句, 防止递归两次null改变两次输入值 if (root.left == null && root.right == null) { return root; } 先序遍历 阅读全文
posted @ 2017-08-01 10:49 apanda009 阅读(130) 评论(0) 推荐(0)
摘要:这道题让我们求二叉树的坡度,某个结点的坡度的定义为该结点的左子树之和与右子树之和的差的绝对值,这道题让我们求所有结点的坡度之和. 这道题最好的解法应该是用后序遍历来做,因为后序遍历的顺序是左-右-根,那么就会从叶结点开始处理,这样我们就能很方便的计算结点的累加和,同时也可以很容易的根据子树和来计算t 阅读全文
posted @ 2017-07-31 22:07 apanda009 阅读(219) 评论(0) 推荐(0)
摘要:用的list.addFist(); 达到左右根的目的. 节点值在加的时候, 先判断是否为空. listNode 也是哦 阅读全文
posted @ 2017-07-31 21:42 apanda009 阅读(96) 评论(0) 推荐(0)
摘要:算法: 根右左, 尾递归 容器: list 尾递归 corner case: 右没有怎么办, 加一个输入值表示深度, list.size() 也表示深度 The core idea of this algorithm: 1.Each depth of the tree only select one 阅读全文
posted @ 2017-07-31 21:37 apanda009 阅读(120) 评论(0) 推荐(0)
摘要:分治法, 自己画个简图(三个, 两个节点), 遍历一下 会写树的高度, 在树的高度上加了个判断而已.别忘了左子树和右子树也是要查的 分治法主要在如何设计返回值, 和题意与返回值的转化, 递归出口1(判空), 最后的节点的出口2(或许加判断等), 分, 合: 将分好的左右节点作为单个节点, 进行题意的 阅读全文
posted @ 2017-07-26 09:26 apanda009 阅读(135) 评论(0) 推荐(0)
摘要:public class MyNode { TreeNode node; int start; int end; public MyNode(TreeNode node, int start, int end) { this.node = node; this.start = start; this 阅读全文
posted @ 2017-07-25 22:39 apanda009 阅读(188) 评论(0) 推荐(0)
摘要:递归出口1, 出口2(操作, 遍历(dfs), 操作), 操作, 遍历(dfs), 操作 阅读全文
posted @ 2017-07-25 18:36 apanda009 阅读(131) 评论(0) 推荐(0)
摘要:从任意一点dfs, 和dfs return 当前, 左节点, 右节点的模板 The basic idea is to subtract the value of current node from sum until the subtraction equals 0, then we know th 阅读全文
posted @ 2017-07-25 13:31 apanda009 阅读(135) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/count-complete-tree-nodes/#/description http://www.cnblogs.com/EdwardLiu/p/5058570.html 递归树高法 复杂度 时间 O(N) 空间 O(1) 思路 完全二 阅读全文
posted @ 2017-06-30 11:25 apanda009 阅读(151) 评论(0) 推荐(0)