版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kaikai_sk/article/details/82938797
二叉树的镜像
LeetCode 226
class Solution
{
/**
* 层次遍历
* @param root
* @return
*/
public TreeNode invertTree_bfs(TreeNode root)
{
if(root == null)
{
return root;
}
else
{
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty())
{
TreeNode pNode = queue.poll();
TreeNode tempNode = pNode.left;
pNode.left = pNode.right;
pNode.right = tempNode;
if(pNode.left!=null)
{
queue.add(pNode.left);
}
if(pNode.right!=null)
{
queue.add(pNode.right);
}
}
return root;
}
}
/**
* 先序遍历的方法
* @param root
* @return
*/
public TreeNode invertTree(TreeNode root)
{
if(root!=null)
{
TreeNode tempNode = root.left;
root.left = root.right;
root.right = tempNode;
invertTree(root.left);
invertTree(root.right);
return root;
}
return null;
}
}
二叉树的先序遍历
public ArrayList<Integer> preorderTraversal(TreeNode root)
{
ArrayList<Integer> path_res = new ArrayList<>();
Stack<TreeNode> stack = new Stack();
if(root == null)
return path_res;
stack.push(root);
while(!stack.isEmpty())
{
TreeNode node = stack.pop();
path_res.add(node.val);
if(node.right!=null)
stack.push(node.right);
if(node.left!=null)
stack.push(node.left);
}
return path_res;
}
二叉树的后序遍历
import java.awt.List;
import java.lang.Thread.State;
import java.util.ArrayList;
import java.util.Stack;
// * Definition for binary tree
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution
{
public static void main(String[] args)
{
TreeNode root = new TreeNode(1);
root.left = null;
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
ArrayList<Integer> res = new Solution().postorderTraversal(root);
System.out.println(res);
}
/**
*不使用递归解答,这才是最简单而且高效的方法吧。
*从后往前观察后序遍历后的结果就明白思路了。
*/
public ArrayList<Integer> postorderTraversal(TreeNode root)
{
ArrayList<Integer> path_res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null)
{
return path_res;
}
stack.push(root);
while(!stack.isEmpty())
{
TreeNode node = stack.pop();
path_res.add(0,node.val);
if(node.left!=null)
stack.push(node.left);
if(node.right!=null)
stack.push(node.right);
}
return path_res;
}
}
//public class Solution
//{
// public ArrayList<Integer> postorderTraversal(TreeNode root)
// {
// ArrayList<Integer> path_res = new ArrayList<>();
// myPostOrder(root, path_res);
// return path_res;
// }
//
// public void myPostOrder(TreeNode root,ArrayList<Integer> path)
// {
// if(root!=null)
// {
// myPostOrder(root.left,path);
// myPostOrder(root.right,path);
// path.add(root.val);
// }
// }
//
//}
minimum depth of binary tree
题目描述
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public int run(TreeNode root)
{
if(root == null)
return 0;
if(root.left ==null && root.right == null)
return 1;
if(root.left==null)
return run(root.right) + 1;
if(root.right==null)
return run(root.left) + 1;
return Math.min(run(root.left),run(root.right))+1;
}
}