当前位置:   article > 正文

Leetcode tree题型总结_leetcode tree 总结

leetcode tree 总结

对我来说,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的第一个的位置










声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/788842
推荐阅读
相关标签
  

闽ICP备14008679号