java二叉树代码_Java二叉树路径和代码示例

给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。

一个有效的路径,指的是从根节点到叶节点的路径。

样例

给定一个二叉树,和 目标值 = 5:

1

/ \

2 4

/ \

2 3

返回:

[

[1, 2, 2],

[1, 4]

]

代码如下:

/**

* Definition of TreeNode:

* public class TreeNode {

* public int val;

* public TreeNode left, right;

* public TreeNode(int val) {

* this.val = val;

* this.left = this.right = null;

* }

* }

*/

public class Solution {

/**

* @param root the root of binary tree

* @param target an integer

* @return all valid paths

*/

public List> binaryTreePathSum(TreeNode root, int target) {

// Write your code here

return dfs(root,new ArrayList(),0,new ArrayList>(),target);

}

public List> dfs(TreeNode root,List node, int sum, List> paths,int target)

{

if(root==null)

{

return new ArrayList>();

}

List> path=new ArrayList>();

if(root.left!=null)

{

List nodes=new ArrayList();

if(node!=null)

{

nodes.addAll(node);

}

nodes.add(root.val);

List> temp=dfs(root.left,nodes,sum+root.val,paths,target);

if(temp!=null)

{

path.addAll(temp);

}

}

if(root.right!=null)

{

List nodes=new ArrayList();

if(node!=null)

{

nodes.addAll(node);

}

nodes.add(root.val);

List> temp=dfs(root.right,nodes,sum+root.val,paths,target);

if(temp!=null)

{

path.addAll(temp);

}

}

if(root.left==null&&root.right==null)

{

List nodes=new ArrayList();

if(node!=null)

{

nodes.addAll(node);

}

nodes.add(root.val);

if(sum+root.val==target)

{

path.add(nodes);

} else{

path=new ArrayList>();

}

}

return path;

}

}

referance

java编程求二叉树最大路径问题代码分析

java中继承测试代码分析

总结

以上就是本文关于Java二叉树路径和代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java的平衡二叉树实现通常是基于红黑树算法,这是一种自平衡二叉查找树。在红黑树,每个节点都被标记为红色或黑色,并满足以下特性: 1. 根节点是黑色的。 2. 所有子节点都是黑色的(子节点为NIL节点)。 3. 如果一个节点是红色的,则它的两个子节点都是黑色的。 4. 从任意节点到其每个子节点的所有路径都包含相同数目的黑色节点。 这些特性确保了红黑树是平衡的,因为任意路径上的黑色节点数目相同。插入和删除操作可以通过对颜色和结构进行适当的旋转来维护树的平衡性。 下面是一个简单的Java平衡二叉树实现示例: ``` import java.util.*; public class RedBlackTree<K extends Comparable<K>, V> { private static final boolean RED = true; private static final boolean BLACK = false; private Node root; private class Node { K key; V val; Node left, right; int size; boolean color; public Node(K key, V val, boolean color, int size) { this.key = key; this.val = val; this.color = color; this.size = size; } } private boolean isRed(Node x) { if (x == null) return false; return x.color == RED; } private int size(Node x) { if (x == null) return 0; return x.size; } public int size() { return size(root); } public boolean isEmpty() { return root == null; } public V get(K key) { if (key == null) throw new IllegalArgumentException("argument to get() is null"); return get(root, key); } private V get(Node x, K key) { while (x != null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else return x.val; } return null; } public boolean contains(K key) { return get(key) != null; } public void put(K key, V val) { if (key == null) throw new IllegalArgumentException("first argument to put() is null"); if (val == null) { delete(key); return; } root = put(root, key, val); root.color = BLACK; } private Node put(Node h, K key, V val) { if (h == null) return new Node(key, val, RED, 1); int cmp = key.compareTo(h.key); if (cmp < 0) h.left = put(h.left, key, val); else if (cmp > 0) h.right = put(h.right, key, val); else h.val = val; // fix-up any right-leaning links if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h); if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if (isRed(h.left) && isRed(h.right)) flipColors(h); h.size = size(h.left) + size(h.right) + 1; return h; } public void deleteMin() { if (isEmpty()) throw new NoSuchElementException("BST underflow"); // if both children of root are black, set root to red if (!isRed(root.left) && !isRed(root.right)) root.color = RED; root = deleteMin(root); if (!isEmpty()) root.color = BLACK; } private Node deleteMin(Node h) { if (h.left == null) return null; if (!isRed(h.left) && !isRed(h.left.left)) h = moveRedLeft(h); h.left = deleteMin(h.left); return balance(h); } public void deleteMax() { if (isEmpty()) throw new NoSuchElementException("BST underflow"); // if both children of root are black, set root to red if (!isRed(root.left) && !isRed(root.right)) root.color = RED; root = deleteMax(root); if (!isEmpty()) root.color = BLACK; } private Node deleteMax(Node h) { if (isRed(h.left)) h = rotateRight(h); if (h.right == null) return null; if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h); h.right = deleteMax(h.right); return balance(h); } public void delete(K key) { if (key == null) throw new IllegalArgumentException("argument to delete() is null"); if (!contains(key)) return; // if both children of root are black, set root to red if (!isRed(root.left) && !isRed(root.right)) root.color = RED; root = delete(root, key); if (!isEmpty()) root.color = BLACK; } private Node delete(Node h, K key) { if (key.compareTo(h.key) < 0) { if (!isRed(h.left) && !isRed(h.left.left)) h = moveRedLeft(h); h.left = delete(h.left, key); } else { if (isRed(h.left)) h = rotateRight(h); if (key.compareTo(h.key) == 0 && (h.right == null)) return null; if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h); if (key.compareTo(h.key) == 0) { Node x = min(h.right); h.key = x.key; h.val = x.val; h.right = deleteMin(h.right); } else h.right = delete(h.right, key); } return balance(h); } private Node rotateRight(Node h) { assert (h != null) && isRed(h.left); Node x = h.left; h.left = x.right; x.right = h; x.color = x.right.color; x.right.color = RED; x.size = h.size; h.size = size(h.left) + size(h.right) + 1; return x; } private Node rotateLeft(Node h) { assert (h != null) && isRed(h.right); Node x = h.right; h.right = x.left; x.left = h; x.color = x.left.color; x.left.color = RED; x.size = h.size; h.size = size(h.left) + size(h.right) + 1; return x; } private void flipColors(Node h) { assert !isRed(h) && isRed(h.left) && isRed(h.right); h.color = RED; h.left.color = BLACK; h.right.color = BLACK; } private Node moveRedLeft(Node h) { assert (h != null); assert isRed(h) && !isRed(h.left) && !isRed(h.left.left); flipColors(h); if (isRed(h.right.left)) { h.right = rotateRight(h.right); h = rotateLeft(h); flipColors(h); } return h; } private Node moveRedRight(Node h) { assert (h != null); assert isRed(h) && !isRed(h.right) && !isRed(h.right.left); flipColors(h); if (isRed(h.left.left)) { h = rotateRight(h); flipColors(h); } return h; } private Node balance(Node h) { assert (h != null); if (isRed(h.right)) h = rotateLeft(h); if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if (isRed(h.left) && isRed(h.right)) flipColors(h); h.size = size(h.left) + size(h.right) + 1; return h; } public K min() { if (isEmpty()) throw new NoSuchElementException("calls min() with empty symbol table"); return min(root).key; } private Node min(Node x) { if (x.left == null) return x; else return min(x.left); } public K max() { if (isEmpty()) throw new NoSuchElementException("calls max() with empty symbol table"); return max(root).key; } private Node max(Node x) { if (x.right == null) return x; else return max(x.right); } public Iterable<K> keys() { if (isEmpty()) return new LinkedList<K>(); return keys(min(), max()); } public Iterable<K> keys(K lo, K hi) { if (lo == null) throw new IllegalArgumentException("first argument to keys() is null"); if (hi == null) throw new IllegalArgumentException("second argument to keys() is null"); Queue<K> queue = new LinkedList<K>(); keys(root, queue, lo, hi); return queue; } private void keys(Node x, Queue<K> queue, K lo, K hi) { if (x == null) return; int cmplo = lo.compareTo(x.key); int cmphi = hi.compareTo(x.key); if (cmplo < 0) keys(x.left, queue, lo, hi); if (cmplo <= 0 && cmphi >= 0) queue.offer(x.key); if (cmphi > 0) keys(x.right, queue, lo, hi); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值