最近总结了一些数据结构和算法相关的题目,这是第一篇文章,关于二叉树的。
先上二叉树的数据结构:
class TreeNode{
int val=0;
//左孩子
TreeNode left=null;
//右孩子
TreeNode right=null;
public TreeNode(int val) {
this.val = val;
}
}
二叉树的题目普遍可以用递归和迭代的方式来解
int maxDeath(TreeNode node){
if(node==null){
return 0;
}
int left = maxDeath(node.left);
int right = maxDeath(node.right);
return Math.max(left,right) + 1;
}
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
int l = minDepth(root.left);
int r = minDepth(root.right);
if (l == 0 || r == 0) {
return l + r + 1;
}
return Math.min(l, r) + 1;
}
}
class Solution {
public int countNodes(TreeNode root) {
if(root==null){
return 0;
}
int l = countNodes(root.left);
int r = countNodes(root.right);
if(l==0||r==0){
return l+r+1;
}
return l+r+1;
}
}
int numsOfNoChildNode(TreeNode root){
if(root == null){
return 0;
}
if(root.left==null&&root.right==null){
return 1;
}
return numsOfNodeTreeNode(root.left)+numsOfNodeTreeNode(root.right);
}
int numsOfkLevelTreeNode(TreeNode root,int k){
if(root == null||k<1){
return 0;
}
if(k==1){
return 1;
}
int numsLeft = numsOfkLevelTreeNode(root.left,k-1);
int numsRight = numsOfkLevelTreeNode(root.right,k-1);
return numsLeft + numsRight;
}
boolean isBalanced(TreeNode node){
return maxDeath2(node)!=-1;
}
int maxDeath2(TreeNode node){
if(node == null){
return 0;
}
int left = maxDeath2(node.left);
int right = maxDeath2(node.right);
if(left==-1||right==-1||Math.abs(left-right)>1){
return -1;
}
return Math.max(left, right) + 1;
}
boolean isCompleteTreeNode(TreeNode root){
if(root == null){
return false;
}
Queue queue = new LinkedList();
queue.add(root);
boolean result = true;
boolean hasNoChild = false;
while(!queue.isEmpty()){
TreeNode current = queue.remove();
if(hasNoChild){
if(current.left!=null||current.right!=null){
result = false;
break;
}
}else{
if(current.left!=null&¤t.right!=null){
queue.add(current.left);
queue.add(current.right);
}else if(current.left!=null&¤t.right==null){
queue.add(current.left);
hasNoChild = true;
}else if(current.left==null&¤t.right!=null){
result = false;
break;
}else{
hasNoChild = true;
}
}
}
return result;
}
boolean isSameTreeNode(TreeNode t1,TreeNode t2){
if(t1==null&&t2==null){
return true;
}
else if(t1==null||t2==null){
return false;
}
if(t1.val != t2.val){
return false;
}
boolean left = isSameTreeNode(t1.left,t2.left);
boolean right = isSameTreeNode(t1.right,t2.right);
return left&&right;
}
boolean isMirror(TreeNode t1,TreeNode t2){
if(t1==null&&t2==null){
return true;
}
if(t1==null||t2==null){
return false;
}
if(t1.val != t2.val){
return false;
}
return isMirror(t1.left,t2.right)&&isMirror(t1.right,t2.left);
}
解题思路:除了根结点之外,剩下的左右子节点互为镜像树。
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirrorTree(root,root);
}
public boolean isMirrorTree(TreeNode t1,TreeNode t2){
if(t1==null&&t2==null) return true;
if(t1==null||t2==null) return false;
if(t1.val!=t2.val) return false;
return isMirrorTree(t1.left,t2.right)&&isMirrorTree(t1.right,t2.left);
}
}
TreeNode mirrorTreeNode(TreeNode root){
if(root == null){
return null;
}
TreeNode left = mirrorTreeNode(root.left);
TreeNode right = mirrorTreeNode(root.right);
root.left = right;
root.right = left;
return root;
}
TreeNode getLastCommonParent(TreeNode root,TreeNode t1,TreeNode t2){
if(findNode(root.left,t1)){
if(findNode(root.right,t2)){
return root;
}else{
return getLastCommonParent(root.left,t1,t2);
}
}else{
if(findNode(root.left,t2)){
return root;
}else{
return getLastCommonParent(root.right,t1,t2)
}
}
}
// 查找节点node是否在当前 二叉树中
boolean findNode(TreeNode root,TreeNode node){
if(root == null || node == null){
return false;
}
if(root == node){
return true;
}
boolean found = findNode(root.left,node);
if(!found){
found = findNode(root.right,node);
}
return found;
}
迭代解法
class Solution {
public List preorderTraversal(TreeNode root) {
List list = new ArrayList();
Stack s = new Stack();
if(root==null){
return list;
}
s.push(root);
while(!s.isEmpty()){
TreeNode tmp = s.pop();
list.add(tmp.val);
if(tmp.right!=null)
s.push(tmp.right);
if(tmp.left!=null)
s.push(tmp.left);
}
return list;
}
}
递归解法
ArrayList preOrderReverse(TreeNode root){
ArrayList result = new ArrayList();
preOrder2(root,result);
return result;
}
void preOrder2(TreeNode root,ArrayList result){
if(root == null){
return;
}
result.add(root.val);
preOrder2(root.left,result);
preOrder2(root.right,result);
}
class Solution {
public List inorderTraversal(TreeNode root) {
List list = new ArrayList();
Stack s = new Stack();
TreeNode current = root;
while(current!=null||!s.isEmpty()){
while(current!=null){
s.push(current);
current = current.left;
}
current = s.pop();
list.add(current.val);
current = current.right;
}
return list;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List postorderTraversal(TreeNode root) {
Stack s = new Stack();
List list = new ArrayList();
if(root==null){
return list;
}
s.push(root);
while(!s.isEmpty()){
TreeNode tmp = s.pop();
list.add(tmp.val);
if(tmp.left!=null){
s.push(tmp.left);
}
if(tmp.right!=null){
s.push(tmp.right);
}
}
Collections.reverse(list);
return list;
}
}
ArrayList postOrder(TreeNode root){
ArrayList list = new ArrayList();
if(root == null){
return list;
}
list.addAll(postOrder(root.left));
list.addAll(postOrder(root.right));
list.add(root.val);
return list;
}
TreeNode buildTreeNode(int[] preorder,int[] inorder){
if(preorder.length!=inorder.length){
return null;
}
return myBuildTree(inorder,0,inorder.length-1,
preorder,0,preorder.length-1);
}
TreeNode myBuildTree(int[] inorder,int instart,
int inend,int[] preorder,int prestart,int preend){
if(instart>inend){
return null;
}
TreeNode root = new TreeNode(preorder[prestart]);
int position = findPosition(inorder,instart,
inend,preorder[start]);
root.left = myBuildTree(inorder,instart,
position-1,preorder,prestart+1,
prestart+position-instart);
root.right = myBuildTree(inorder,position+1,
inend,preorder,position-inend+preend+1,preend);
return root;
}
int findPosition(int[] arr,int start,int end,int key){
int i;
for(i = start;i<=end;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
TreeNode insertNode(TreeNode root,TreeNode node){
if(root == node){
return node;
}
TreeNode tmp = new TreeNode();
tmp = root;
TreeNode last = null;
while(tmp!=null){
last = tmp;
if(tmp.val>node.val){
tmp = tmp.left;
}else{
tmp = tmp.right;
}
}
if(last!=null){
if(last.val>node.val){
last.left = node;
}else{
last.right = node;
}
}
return root;
}
void findPath(TreeNode r,int i){
if(root == null){
return;
}
Stack stack = new Stack();
int currentSum = 0;
findPath(r, i, stack, currentSum);
}
void findPath(TreeNode r,int i,Stack stack,int currentSum){
currentSum+=r.val;
stack.push(r.val);
if(r.left==null&&r.right==null){
if(currentSum==i){
for(int path:stack){
System.out.println(path);
}
}
}
if(r.left!=null){
findPath(r.left, i, stack, currentSum);
}
if(r.right!=null){
findPath(r.right, i, stack, currentSum);
}
stack.pop();
}
给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点。找到树中所有值在
k1 到 k2 范围内的节点。即打印所有x (k1 <= x <= k2) 其中 x 是二叉查找树的中的节点值。
返回所有升序的节点值。
ArrayList result;
ArrayList searchRange(TreeNode root,int k1,int k2){
result = new ArrayList();
searchHelper(root,k1,k2);
return result;
}
void searchHelper(TreeNode root,int k1,int k2){
if(root == null){
return;
}
if(root.val>k1){
searchHelper(root.left,k1,k2);
}
if(root.val>=k1&&root.val<=k2){
result.add(root.val);
}
if(root.val
ArrayList> levelOrder(TreeNode root){
ArrayList> result = new ArrayList>();
if(root == null){
return result;
}
Queue queue = new LinkedList();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
ArrayList< level = new ArrayList():
for(int i = 0;i < size ;i++){
TreeNode node = queue.poll();
level.add(node.val);
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
result.add(Level);
}
return result;
}
二叉树中两个节点的最长距离可能有三种情况:
1.左子树的最大深度+右子树的最大深度为二叉树的最长距离
2.左子树中的最长距离即为二叉树的最长距离
3.右子树种的最长距离即为二叉树的最长距离
因此,递归求解即可
private static class Result{
int maxDistance;
int maxDepth;
public Result() {
}
public Result(int maxDistance, int maxDepth) {
this.maxDistance = maxDistance;
this.maxDepth = maxDepth;
}
}
int getMaxDistance(TreeNode root){
return getMaxDistanceResult(root).maxDistance;
}
Result getMaxDistanceResult(TreeNode root){
if(root == null){
Result empty = new Result(0,-1);
return empty;
}
Result lmd = getMaxDistanceResult(root.left);
Result rmd = getMaxDistanceResult(root.right);
Result result = new Result();
result.maxDepth = Math.max(lmd.maxDepth,rmd.maxDepth) + 1;
result.maxDistance = Math.max(lmd.maxDepth + rmd.maxDepth,
Math.max(lmd.maxDistance,rmd.maxDistance));
return result;
}
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?
int numTrees(int n ){
int[] counts = new int[n+2];
counts[0] = 1;
counts[1] = 1;
for(int i = 2;i<=n;i++){
for(int j = 0;j
一棵BST定义为:
节点的左子树中的值要严格小于该节点的值。
节点的右子树中的值要严格大于该节点的值。
左右子树也必须是二叉查找树。
一个节点的树也是二叉查找树。
public int lastVal = Integer.MAX_VALUE;
public boolean firstNode = true;
public boolean isValidBST(TreeNode root) {
// write your code here
if(root==null){
return true;
}
if(!isValidBST(root.left)){
return false;
}
if(!firstNode&&lastVal >= root.val){
return false;
}
firstNode = false;
lastVal = root.val;
if (!isValidBST(root.right)) {
return false;
}
return true;
}