1、二叉搜索树最近公共祖先
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root==null) {
return root;
}
while(root!=null){
if ((p.val<=root.val&&q.val>=root.val)||(p.val>=root.val&&q.val<=root.val)) {
return root;
}
if (p.val<=root.val&&q.val<=root.val) {
root=root.left;
continue;
}
if (p.val>=root.val&&q.val>=root.val) {
root=root.right;
continue;
}
}
return root;
}
2、普通二叉树最近公共祖先
public static TreeNode lowestCommonAncestor3(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q)
return root;
TreeNode left = lowestCommonAncestor3(root.left, p, q);
TreeNode right = lowestCommonAncestor3(root.right, p, q);
if (left == null) {
return right;
}else {
if (right==null) {
return left;
}else {
return root;
}
}
// return left == null ? right : right == null ? left : root;
}
public static TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
if (root==null) {
return root;
}
while(root!=null){
List<TreeNode> pList=preOrder1(root, p);
List<TreeNode> qList=preOrder1(root, q);
int length=pList.size()<qList.size()?pList.size():qList.size();
for (int i = 0; i < length; i++) {
if (pList.get(i).val!=qList.get(i).val) {
return pList.get(i-1);
}
}
}
return root;
}
/**
* 前序
* LANG
* @param Node
*/
public static List<TreeNode> preOrder1(TreeNode Node,TreeNode input) {
Stack<TreeNode> stack = new Stack<>();
List<TreeNode> list=new ArrayList<>();
while (Node != null || !stack.empty()) {
while (Node != null) {
// System.out.print(Node.val + " ");
list.add(Node);
if (Node.val==input.val) {
break;
}
stack.push(Node);
Node = Node.left;
}
if (!stack.empty()) {
Node = stack.pop();
Node = Node.right;
}
}
return list;
}
3、遍历
/**
* 中序遍历
* LANG
* @param Node
*/
public static void midOrder1(TreeNode Node) {
Stack<TreeNode> stack = new Stack<>();
while (Node != null || !stack.empty()) {
while (Node != null) {
stack.push(Node);
Node = Node.left;
}
if (!stack.empty()) {
Node = stack.pop();
System.out.print(Node.val + " ");
Node = Node.right;
}
}
}
/**
* 后序遍历
* 非递归
*/
public static void posOrder1(TreeNode Node) {
Stack<TreeNode> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
int i = 1;
while (Node != null || !stack1.empty()) {
while (Node != null) {
stack1.push(Node);
stack2.push(0);
Node = Node.left;
}
while (!stack1.empty() && stack2.peek() == i) {
stack2.pop();
TreeNode Node1 = stack1.pop();
System.out.print(Node1.val + " ");
}
if (!stack1.empty()) {
stack2.pop();
stack2.push(1);
Node = stack1.peek();
Node = Node.right;
}
}
}
/*
* 层序遍历
* 非递归
*/
public static void levelOrder1(TreeNode Node) {
if (Node == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(Node);
while (queue.size() != 0) {
Node = queue.poll();
System.out.print(Node.val + " ");
if (Node.left != null) {
queue.offer(Node.left);
}
if (Node.right != null) {
queue.offer(Node.right);
}
}
}
4、最大深度
public static int maxDepth(TreeNode root) {
int left=0,right=0;
int dep=0;
if (root!=null) {
dep++;
left=maxDepth(root.left);
right=maxDepth(root.right);
return left>right?left+1:right+1;
}
return 0;
}
5、删除二叉搜索树的其中一个给定节点,找到节点后,需要连起来
public static TreeNode deleteNode(TreeNode root, int key) {
if (root==null||root.val==key) {
return deleteNode(root);
}
TreeNode p=root;
while(true){
if (p.val>key) {
if (p.left==null||p.left.val==key) {
p.left=deleteNode(p.left);
break;
}
p=p.left;
}else {
if (p.right==null||p.right.val==key) {
p.right=deleteNode(p.right);
break;
}
p=p.right;
}
}
return root;
}
public static TreeNode deleteNode(TreeNode root){
if (root==null) {
return root;
}
if (root.right==null) {
return root.left;
}
if (root.left==null) {
return root.right;
}
TreeNode temp=root.right;
while(temp.left!=null){
temp=temp.left;
}
temp.left=root.left;
return root.right;
}