一,定义
一棵二叉树中含有n(n>=0)个节点,当n=0时,他是一棵空二叉树;
当n>0时,他由一个根节点和两棵互不相交的称为左子树和右子树的二叉树组成.
** 二叉树的定义也是递归的 **
二,二叉树常见的题目
一般来说,二叉树的题目大部分都可以通过遍历和他的递归定义以及搜索来解决
-
1,遍历
先序,中序,后序,层次遍历
先序遍历LeetCode 144. Binary Tree Preorder Traversal
中序遍历LeetCode 94. Binary Tree Inorder Traversal
后序遍历LeetCode 145. Binary Tree Postorder Traversal
层次遍历LeetCode 102. Binary Tree Level Order Traversal -
2,与遍历相关的(也就是利用遍历可以解决的题目)
判断二叉排序树是否合法---利用中序遍历LeetCode 98. Validate Binary Search Tree
二叉树"之"字形遍历---层次遍历LeetCode 103. Binary Tree Zigzag Level Order Traversal
二叉树最大高度---层次,后序遍历LeetCode 104. Maximum Depth of Binary Tree
二叉树层次遍历二---层次遍历LeetCode 107. Binary Tree Level Order Traversal II
路径和---层次,后序遍历LeetCode 112. Path Sum
路径和---层次,后序遍历LeetCode 113. Path Sum II
叶子节点和---层次,后序遍历LeetCode129. Sum Root to Leaf Numbers
从右看二叉树---层次LeetCode 199. Binary Tree Right Side View -
3,利用定义或者搜索
判断两棵二叉树是否相同LeetCode 100. Same Tree
二叉树镜像LeetCode 101. Symmetric Tree
二叉树最大高度LeetCode 104. Maximum Depth of Binary Tree
路径和LeetCode 112. Path Sum
路径和LeetCode 113. Path Sum II
翻转二叉树LeetCode 226. Invert Binary Tree -
4,二叉树的序列化和反序列化
LeetCode 297. Serialize and Deserialize Binary Tree -
5,二叉树的构建
先序和中序遍历 或者 后序和中序遍历可以唯一确定一棵二叉树
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
网友评论