Leetcode tree题型总结

11 篇文章 0 订阅
3 篇文章 0 订阅

对我来说,Tree 是 Leetcode中最简单的题,因为Tree的题型比较单一,方法也比较固定。


98. Validate Binary Search Tree https://leetcode.com/problems/validate-binary-search-tree/我觉得这道题和Symmetric Tree比较像都是考察树的结构,要判断这个需要判断左右节点,采用从底到上,以Root为开始,depth-first-search执行得到对所有的结果的判断。


96. Unique Binary Search Trees

https://leetcode.com/problems/unique-binary-search-trees/ 这道题有公式


95. Unique Binary Search Trees II???


101. Symmetric Tree https://leetcode.com/problems/symmetric-tree/


129. Sum Root to Leaf Numbers https://leetcode.com/problems/sum-root-to-leaf-numbers/先序遍历


100. Same Tree

https://leetcode.com/problems/same-tree/


99. Recover Binary Search Tree https://leetcode.com/problems/recover-binary-search-tree/???


116. Populating Next Right Pointers in Each Node

https://leetcode.com/problems/populating-next-right-pointers-in-each-node/??


117. Populating Next Right Pointers in Each Node II

https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/


111. Minimum Depth of Binary Tree

https://leetcode.com/problems/minimum-depth-of-binary-tree/

同path sum, 利用先序遍历记录树的信息。


104. Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/

同样利用先序遍历,记录树的信息。


236. Lowest Common Ancestor of a Binary Tree

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/

遍历左右子树,如果左右孩子不为空,说明遍历到,则认为找到了最小的孩子。


235. Lowest Common Ancestor of a Binary Search Tree

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

如果要找的节点一个是大于当前节点,一个小于当前节点,则现在的节点就是要找的值,否则移向同一侧。


230. Kth Smallest Element in a BST

https://leetcode.com/problems/kth-smallest-element-in-a-bst/

Binary Search Tree的中序遍历的结果是从小到大的顺序,所以记录现在遍历到的节点的值,如果等于Target 就返回该节点。


110. Balanced Binary Tree

https://leetcode.com/problems/balanced-binary-tree/

记录树的高度,并判断左右子树的高度的差的绝对值,如果大于1则不是Balanced Binary tree.从顶往底依次遍历。


144. Binary Tree Preorder Traversal

https://leetcode.com/problems/binary-tree-preorder-traversal/


145. Binary Tree Postorder Traversal

https://leetcode.com/problems/binary-tree-postorder-traversal/


94. Binary Tree Inorder Traversal

https://leetcode.com/problems/binary-tree-inorder-traversal/

三个顺序的Tree的traverse顺序,只是在help函数中访问节点的顺序不同。


102. Binary Tree Level Order Traversal

https://leetcode.com/problems/binary-tree-level-order-traversal/

利用queue进行层序遍历



107. Binary Tree Level Order Traversal II

倒序的层序遍历,每次把结果加到Result的第一个的位置










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值