前记
目录
求二叉树中节点的最大距离
《编程之美-3.8》 P241
重建二叉树
《剑指offer-面试题6》P55 《编程之美-3.9》P246
分层遍历二叉树
《编程之美-3.10》P252
树的子结构
《剑指offer-面试题18》P117
二叉树的镜像
《剑指offer-面试题19》P125
从上往下打印二叉树
《剑指offer-面试题23》 P137
二叉搜索树的后序遍历序列
《剑指offer-面试题24》P140
二叉树中和为某一值的路径
《剑指offer-面试题6》P143
二叉搜索树与双向链表
《剑指offer-面试题27》P151
二叉树的深度
《剑指offer-面试题39》P207
LeetCode-102Binary Tree Level Order Traversal
LeetCode-107 Binary Tree Level Order Traversal II
LeetCode-98 Validate Binary Search Tree
LeetCode-99 Recover Binary Search Tree
LeetCode-100 Same Tree
LeetCode-101 Symmetric Tree
LeetCode-104 Maximum Depth of Binary Tree
LeetCode-108 Convert Sorted Array to Binary Search Tree
LeetCode-110 Balanced Binary Tree
LeetCode-111 Minimum Depth of Binary Tree
LeetCode-112 Path Sum
LeetCode-113 Path Sum II
LeetCode-116 Populating Next Right Pointers in Each Node
LeetCode-117 Populating Next Right Pointers in Each Node II
LeetCode-124 Binary Tree Maximum Path Sum
LeetCode-129 Sum Root to Leaf Numbers
LeetCode-95 Unique Binary Search Trees II
LeetCode-96 Unique Binary Search Trees
LeetCode-94 Binary Tree Inorder Traversal
LeetCode-114 Flatten Binary Tree to Linked List
LeetCode-103 Binary Tree Zigzag Level Order Traversal
LeetCode-105 Construct Binary Tree from Preorder and Inorder Tree
LeetCode-106 Construct Binary Tree from Inorder and Postorder Tree
正文
求二叉树中节点的最大距离
GVEdit
digraph G{ 1-> 2;1-> 3; 2->4;2->5; 3->6; 3->7; 4->A; 6->B }
重建二叉树
struct NODE{
NODE* pLeft;
NODE* pRight;
char chValue; //it can be other data type
}
void Rebuild(char* pPreOrder, char*pInOrder, int nTreeLen, NODE** pRoot)
假如已经有了前序遍历和中序遍历的结果,希望通过一个算法重建。
example
GVEdit
digraph G{ a-> b;a-> c; c->e;c->f; b->d; }
分层遍历二叉树
《编程之美-3.10》P252digraph G{ 1-> 2;1-> 3; 2->4;2->5; 3->6; 5->7;5->8; }
树的子结构
《剑指offer-面试题18》P117struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
二叉树的镜像
《剑指offer-面试题19》P125struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
从上往下打印二叉树
《剑指offer-面试题23》 P137struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
二叉搜索树的后序遍历序列
《剑指offer-面试题24》P140二叉树中和为某一值的路径
《剑指offer-面试题6》P143struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
二叉搜索树与双向链表
《剑指offer-面试题27》P151struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
GVEdit
digraph G{ 10-> 6;10-> 14; 6->4;6->8; 14->12; 14->16; }
digraph G{ 4->6;6->4; 8->6;6->8; 8->10;10->8; 10->12;12->10; 14->12;12->14; 14->16;16->14; }
二叉树的深度
《剑指offer-面试题39》P207struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
LeetCode-102 Binary Tree Level Order Traversal
LeetCode-107 Binary Tree Level Order Traversal II
LeetCode-98 Validate Binary Search Tree
LeetCode-99 Recover Binary Search Tree
LeetCode-100Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
LeetCode-101Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
.
LeetCode-104 Maximum Depth of Binary Tree
LeetCode-108 Convert Sorted Array to Binary Search Tree
LeetCode-110 Balanced Binary Tree
LeetCode-111 Minimum Depth of Binary Tree
LeetCode-112 Path Sum
LeetCode-113 Path Sum II
LeetCode-116 Populating Next Right Pointers in Each Node
LeetCode-117 Populating Next Right Pointers in Each Node II
LeetCode-124 Binary Tree Maximum Path Sum
LeetCode-129 Sum Root to Leaf Numbers
LeetCode-95 Unique Binary Search Trees II
LeetCode-96 Unique Binary Search Trees
LeetCode-94 Binary Tree Inorder Traversal
LeetCode-114 Flatten Binary Tree to Linked List
LeetCode-103 Binary Tree Zigzag Level Order Traversal
LeetCode-105 Construct Binary Tree from Preorder and Inorder Tree
LeetCode-106 Construct Binary Tree from Inorder and Postorder Tree
参考
- 《剑指offer》何海涛
- 《编程之美》邹欣等