博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实现二叉树的深度计算
阅读量:7026 次
发布时间:2019-06-28

本文共 2539 字,大约阅读时间需要 8 分钟。

hot3.png

尝试不同方法求二叉树的深度:

1.depth1,递归计算二叉树的深度,根结点的深度=max(左子树的深度,右子树的深度) + 1。

2.depth2,访问左结点,如有右结点则压栈1,同时把右结点的深度压栈2,没有左结点时表示该次遍历完成,记录深度;从栈1取出结点,栈2取出该结点的深度,再次遍历。

3.depth3,利用层序遍历的思想,每完成一层的遍历就给一个标记。

package com.devchao.tree;import java.util.LinkedList;import java.util.Queue;import java.util.Stack;public class BinaryTree {	public static void main(String[] args) {				Node n15 = new Node(15, null, null);		Node n14 = new Node(14, n15, null);		Node n13 = new Node(13, null, n14);		Node n12 = new Node(12, n13, null);		Node n11 = new Node(11, null, n12);				Node n5 = new Node(5, null, null);		Node n6 = new Node(6, null, null);		Node n8 = new Node(8, null, null);		Node n9 = new Node(9, n11, null);		Node n10 = new Node(10, null, null);		Node n7 = new Node(7, n8, null);		Node n4 = new Node(4, n6, n7);		Node n3 = new Node(3, null, n5);		Node n2 = new Node(2, n9, n10);		Node n1 = new Node(1, n3, n4);		Node root = new Node(0, n1, n2);		System.out.println(depth3(root));	}		public static int depth1(Node root){		int dep1 = 1, dep2 = 1;		if(root.left != null) {			dep1 = dep1 + depth1(root.left);		}		if(root.right != null) {			dep2 = dep2 + depth1(root.right);		}				System.out.println(root.data + " " + dep1 + " " + dep2);		return dep1 >= dep2 ? dep1 : dep2;	}		public static int depth2(Node root){		int dep = 1, tempDep = 1;		Stack
q = new Stack
(); Stack
dq = new Stack
(); Node node = root; while(node != null || !q.isEmpty()) { if(node == null) { node = q.pop(); dep = dep < tempDep ? tempDep : dep; tempDep = dq.pop(); } if(node.right != null) { dq.add(tempDep + 1); q.add(node.right); } if(node.left != null) { tempDep = tempDep + 1; } node = node.left; } return dep; } public static int depth3(Node root){ int dep = 0, tempTag = 0; Queue
queue = new LinkedList<>(); queue.add(root); Queue
tagQueue = new LinkedList<>(); tagQueue.add(1); while(!queue.isEmpty()) { root = queue.poll(); if(root.left != null) { queue.add(root.left); tagQueue.add(0); } if(root.right != null) { queue.add(root.right); tagQueue.add(0); } tempTag = tagQueue.poll(); if(tempTag == 1) { dep = dep + 1; if(!tagQueue.isEmpty()) { tagQueue.remove(); tagQueue.add(1); } } } return dep; } static class Node { public int data; public Node left; public Node right; public Node(int data, Node left, Node right) { this.data = data; this.left = left; this.right = right; } }}

 

转载于:https://my.oschina.net/lizhenchao/blog/867875

你可能感兴趣的文章
【Android市场】提交应用的一点经验分享
查看>>
SQL Server中事务处理的注意事项
查看>>
MAC使用小技巧
查看>>
Hibernate配置详细解释(转 )
查看>>
JS操作节点
查看>>
svn 的patch p0和p1有什么区别
查看>>
python&&Java&&jsp+servlet连接数据库报错收藏(sql server,mysql)
查看>>
远程访问(HttpClient和HttpResponse的使用) 原型模式
查看>>
【poj解题】3664
查看>>
linux 加固措施
查看>>
OpenStack简单测试性能监控数据记录
查看>>
AngularJS中的依赖注入
查看>>
HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
查看>>
八进制
查看>>
python网络爬虫笔记(五)
查看>>
java面试题——中间件&&数据库&&redis(三)
查看>>
batch normalization在测试时的问题
查看>>
PhoneGap 简单使用
查看>>
Python时间和日期
查看>>
二叉树节点的删除
查看>>