面试题-树(持续更新)
2014-04-25 15:18
239 查看
作者:disappearedgod
文章出处:/article/3730103.html
时间:2014-4-25
正文部分是题目的叙述,点击标题(红色为可点击)会链接到解法。
《编程之美-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
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义“距离”为两节点之间边的个数。
写一个程序求一棵二叉树中相距最远的两个节点之间的距离。
给出二叉树的三种遍历次序(前序,中序,后序),如果知道了遍历的结果,能不能把一棵树重新构造出来呢?
example
前序遍历:a b d c e f
中序遍历:d b a e c f
给定一棵二叉树,压球按分层遍历该二叉树,即从上到下按层次访问该二叉树(每一层将单独输出一行),每一层要求访问的顺序为从左到右,并将节点依次编号。分层输出二叉树。
example:
输出为:
1
2 3
4 5 6
7 8
并写出另外一个函数,打印二叉树中某层次的节点(从左到右),其中根节点为第0层,函数原型为int PrintNodeAtLevel(Node* root, int level),成功返回1,失败则返回0.
输入两颗二叉树A和B,判断B是不是A的子结构。
二叉树定义如下:
二叉树的镜像
《剑指offer-面试题19》P125
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
二叉树定义如下:
从上往下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
二叉树定义如下:
题目与《编程之美》层次打印题目一样。
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是返回true,否则返回false。假设输入的数组的任意两个数组都互不相同。
输入一棵二叉树和一个整数,打印出二叉树中节点值的和胃输入整数的所有路径。从树的根节点开始往下一直到叶节点所经历的节点形成一条路径。
二叉树定义如下:
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的节点,职能调整数中节点指针的指向。
二叉树定义如下:
输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点一次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
二叉树定义如下:
题目二:输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
LeetCode-102
LeetCode-107
LeetCode-98
LeetCode-99
Total Accepted: 16148 Total
Submissions: 38995My Submissions
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.
Total Accepted: 12814 Total
Submissions: 40358My Submissions
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what
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:
The above binary tree is serialized as
LeetCode-104
LeetCode-108
LeetCode-110
LeetCode-111
LeetCode-112
LeetCode-113
LeetCode-116
LeetCode-117
LeetCode-124
LeetCode-129
LeetCode-95
LeetCode-96
LeetCode-94
LeetCode-114
LeetCode-103
LeetCode-105
LeetCode-106
《编程之美》邹欣等
文章出处:/article/3730103.html
时间:2014-4-25
前记
这里主要列举一些简单的关于树的练习题。正文部分是题目的叙述,点击标题(红色为可点击)会链接到解法。
目录
求二叉树中节点的最大距离《编程之美-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
正文
求二叉树中节点的最大距离
《编程之美-3.8》 P241如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义“距离”为两节点之间边的个数。
写一个程序求一棵二叉树中相距最远的两个节点之间的距离。
GVEdit digraph G{ 1-> 2;1-> 3; 2->4;2->5; 3->6; 3->7; 4->A; 6->B }
重建二叉树
《剑指offer-面试题6》P55 《编程之美-3.9》P246给出二叉树的三种遍历次序(前序,中序,后序),如果知道了遍历的结果,能不能把一棵树重新构造出来呢?
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
前序遍历:a b d c e f
中序遍历:d b a e c f
GVEdit digraph G{ a-> b;a-> c; c->e;c->f; b->d; }
分层遍历二叉树
《编程之美-3.10》P252给定一棵二叉树,压球按分层遍历该二叉树,即从上到下按层次访问该二叉树(每一层将单独输出一行),每一层要求访问的顺序为从左到右,并将节点依次编号。分层输出二叉树。
example:
digraph G{ 1-> 2;1-> 3; 2->4;2->5; 3->6; 5->7;5->8; }
输出为:
1
2 3
4 5 6
7 8
并写出另外一个函数,打印二叉树中某层次的节点(从左到右),其中根节点为第0层,函数原型为int PrintNodeAtLevel(Node* root, int level),成功返回1,失败则返回0.
树的子结构
《剑指offer-面试题18》P117输入两颗二叉树A和B,判断B是不是A的子结构。
二叉树定义如下:
struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; }
二叉树的镜像
《剑指offer-面试题19》P125请完成一个函数,输入一个二叉树,该函数输出它的镜像。
二叉树定义如下:
struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; }
从上往下打印二叉树
《剑指offer-面试题23》 P137从上往下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
二叉树定义如下:
struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; }
题目与《编程之美》层次打印题目一样。
二叉搜索树的后序遍历序列
《剑指offer-面试题24》P140输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是返回true,否则返回false。假设输入的数组的任意两个数组都互不相同。
二叉树中和为某一值的路径
《剑指offer-面试题6》P143输入一棵二叉树和一个整数,打印出二叉树中节点值的和胃输入整数的所有路径。从树的根节点开始往下一直到叶节点所经历的节点形成一条路径。
二叉树定义如下:
struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; }
二叉搜索树与双向链表
《剑指offer-面试题27》P151输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的节点,职能调整数中节点指针的指向。
二叉树定义如下:
struct 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》P207输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点一次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
二叉树定义如下:
struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; }
题目二:输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
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
Same Tree
Total Accepted: 16148 TotalSubmissions: 38995My Submissions
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
Symmetric Tree
Total Accepted: 12814 TotalSubmissions: 40358My Submissions
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 \ 5
The 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》何海涛《编程之美》邹欣等
相关文章推荐
- 【C++】C++常见面试题汇总_持续更新中...
- 笔试面试题整理(二),持续更新。。。。
- Java面试题总结2(持续更新)
- 笔试面试题整理,慢慢搜集....持续更新
- iOS面试题-持续更新
- 8月面试的几道编程基础题(持续更新各种面试题)
- 计算机网络面试题汇总(持续更新)
- 安卓面试题(持续更新中)
- iOS面试题搜集(持续更新)
- iOS面试题搜集(持续更新)
- javaSE面试题集锦(持续更新)
- iOS 面试题归纳整理—基础篇(持续更新中)
- iOS面试题搜集(持续更新)
- 一些有意思的面试题(持续更新)
- andorid面试题和笔记(持续更新)(framework)
- Java面试题大全(持续更新中)
- 初级前端面试题-持续更新
- 谷歌面试题(持续更新)
- informatica 面试题大全(持续更新)
- 常见的前端面试题——(持续更新...)