算法:二叉树



1. Maximum Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree/

最大深度:

解法1:<Recursive>

1 public class Solution {
2     public int maxDepth(TreeNode root) {
3         if (root == null) return 0;
4         int rst = Math.max(maxDepth(root.left), maxDepth(root.right));
5         return rst + 1;
6     }
7 }

解法2:<Iterative>(层序遍历思想: queue+size+cnt;)

 1 public int maxDepth(TreeNode root) {
 2     if(root == null) {
 3         return 0;
 4     }
 5     Queue<TreeNode> queue = new LinkedList<>();
 6     queue.offer(root);
 7     int count = 0;
 8     while(!queue.isEmpty()) {
 9         int size = queue.size();
10         while(size-- > 0) {
11             TreeNode node = queue.poll();
12             if(node.left != null) {
13                 queue.offer(node.left);
14             }
15             if(node.right != null) {
16                 queue.offer(node.right);
17             }
18         }
19         count++;
20     }
21     return count;
22 }

2. Minimum Depth of Binary Tree:https://leetcode.com/problems/minimum-depth-of-binary-tree/

最小深度:

解法1:<Recursice>(左节点为null返回右边的min+1;右节点为null返回左边的min+1;都不为null则返回Math.min()+1;)

1 public class Solution {
2     public int minDepth(TreeNode root) {
3         if (root == null) return 0;
4         if (root.left == null) return minDepth(root.right) + 1;
5         if (root.right == null) return minDepth(root.left) + 1;
6         int rst = Math.min(minDepth(root.left), minDepth(root.right));
7         return rst + 1;
8     }
9 }

解法2:<Iterative>(层序遍历思想;找到第一个叶节点就返回)

public class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int count = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left == null && node.right == null) return count;
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            count++;
        }
        return count;
    }
}

3. Binary Tree Inorder Traversal:https://leetcode.com/problems/binary-tree-inorder-traversal/

中序遍历:

解法1:<Recursive>(addAll)

public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> rst = new ArrayList<>();
        if (root == null) return rst;
        rst.addAll(inorderTraversal(root.left));
        rst.add(root.val);
        rst.addAll(inorderTraversal(root.right));
        return rst;
    }
}

解法2:<Iterative>

时间: 2024-08-18 02:47:10

算法:二叉树的相关文章

编程算法 - 二叉树的深度 代码(C)

二叉树的深度 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一棵二叉树的根节点, 求该树的深度. 依次选择最深的左右子树, 然后递归加1. 代码: /* * main.cpp * * Created on: 2014.6.12 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #include <stdlib.h> #include <

[算法]二叉树的非递归遍历算法

1.二叉树的非递归中序遍历算法 二叉树的中序遍历方法是:左中右,因此一开始会顺着根节点的左孩子一直往下(这点和先序遍历一样,这也是二者前面部分代码很相似的原因),到最后一个左孩子时尝试把它的右孩子塞进栈内,然后顺着它的的左孩子而下,直到不能访问为止.利用的栈FILO的特性,对每个节点都进行顺左孩子而下即可. 上代码: 1 void inOrder(TreeNode* root,vector<int>& inOrder) 2 { 3 stack<TreeNode*>st; 4

javascript数据结构与算法--二叉树(插入节点、生成二叉树)

javascript数据结构与算法-- 插入节点.生成二叉树 二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ /*用来生成一个节点*/ function Node(data, left, right) { this.data = data;//节点存储的数据 this.left = left; this.right = right; this.show = show; } function sh

[数据结构与算法] 二叉树及其遍历方式

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4289830.html 一.数据结构分类 (一)按逻辑结构 集合(无辑关系) 线性结构(线性表):数组.链表.栈.队列 非线性结构:树.图.多维数组 (二)按存储结构 顺序(数组)储结构.链式储结构.索引储结构.散列储结构 二.二叉树相关性质

数据结构与算法 —— 二叉树

二叉树 定义: 来自于百度百科. 在计算机科学中,二叉树是每个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(right subtree).二叉树常被用于实现二叉查找树和二叉堆. 二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒.二叉树的第i层至多有2^{i-1}个结点:深度为k的二叉树至多有2^k-1个结点:对任何一棵二叉树T,如果其终端结点数为n_0,度为2的结点数为n_

新手学习算法----二叉树(将一个二叉查找树按照中序遍历转换成双向链表)

题目:将一个二叉查找树按照中序遍历转换成双向链表. 给定一个二叉查找树: 4 / 2 5 / 1 3 返回 1<->2<->3<->4<->5. 思路:如果对于当前节点,把右子树转换成双向链表,然后把左子树转换成双向链表,转换的时候我们都标记了链表的头节点和尾节点,那么只需要将当前节点和左子树的尾部相连,和右子树的头部相连即可. Java代码:这个是借鉴九章里面的解题法.但是对于左右子树转换成二叉树也不是很理解,还待需要继续分析. /** * Definit

小甲鱼数据结构和算法-----二叉树的构建和前序遍历

题目要求:建立二叉树并输出每个字符所在的层数.如下图要求输出 A 在第一层 B.C 在第二层 D.E在第三层 代码如下: #include <stdio.h> #include <stdlib.h> typedef struct BiTNode { char data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; // 创建一棵二叉树,约定用户遵照前序遍历的方式输入数据 void CreateBiTree(BiTree *T)

新手学习算法----二叉树(后序遍历)

这种算法只用到了一次入栈一次出栈就可以了, //首先将根节点的左节点所有的节点入栈,一直到叶子节点,先将最左的叶子节点入栈,然后判断是否已到了叶子节点,如果是则将节点的value值入栈,接着将此节点出栈,因为他没用改了 public ArrayList<Integer> postorderTraversal(TreeNode root) { // write your code here Stack<TreeNode> s = new Stack<TreeNode>()

数据结构和算法——二叉树

树1.树的优点有序数组: 查找很快,二分法实现的查找所需要的时间为O(logN),遍历也很快,但是在有序数组中插入,删除却需要先 找到位置, 在把数组部分元素后移,效率并不高. 链表: 链表的插入和删除都是很快速的,仅仅需要改变下引用值就行了,时间仅为O(1),但是在链表中查找数据却需要遍历所有的元素, 这个效率有些慢了.树的优点: 树结合了有序数组和链表的优点,可以实现快速的查找,也可以快速的删除,查找. 树的一些专用术语: 路径: 顺着连接节点的边从一个节点到另一个节点的,所经过的所有节点的

python数据结构与算法——二叉树结构与遍历方法

先序遍历,中序遍历,后序遍历 ,区别在于三条核心语句的位置 层序遍历  采用队列的遍历操作第一次访问根,在访问根的左孩子,接着访问根的有孩子,然后下一层 自左向右一一访问同层的结点 # 先序遍历 # 访问结点,遍历左子树,如果左子树为空,则遍历右子树, # 如果右子树为空,则向上走到一个可以向右走的结点,继续该过程 preorder(t):    if t:       print t.value       preorder t.L       preorder t.R # 中序遍历 # 从根