ZOJ 3805 Machine(二叉树,递归)】的更多相关文章

题意:一颗二叉树,求  “  宽度  ” 思路:递归,貌似这个思路是对的,先记下,但是提交时超时, 1.如果当前节点只有左孩子,那么当前宽度等于左孩子宽度 2.如果当前节点只有右孩子,那么当前宽度等于右孩子宽度 3.如果当前节点既有左孩子又有孩子 3.1两个孩子宽度相等,则当前宽度等于其中一个孩子宽度+1 3.2两个孩子宽度不等,则当前宽度等于两个孩子宽度中大的那一个 代码1:超时 #include<iostream> #include<cstdio> #include<cs…
Machine Time Limit: 2 Seconds      Memory Limit: 65536 KB In a typical assembly line, machines are connected one by one. The first machine's output product will be the second machine's raw material. To simplify the problem, we put all machines into a…
树的定义具有递归特性,因此用递归来遍历比较符合特性,但是用非递归方式就比较麻烦,主要是递归和栈的转换. import java.util.Stack; /** * @author 李文浩 * @version 2017/7/30. */ public class BinaryTree { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } pub…
Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals pre and post are distinct positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=…
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / \ 9 20…
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 / \ 9 2…
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iteratively? ---------------------------------------------------…
二叉树类: package com.antis.tree; public class BinaryTree { int data; //根节点数据 BinaryTree left; //左子树 BinaryTree right; //右子树 public BinaryTree(int data) //实例化二叉树类 { this.data = data; left = null; right = null; } /** * 向二叉树中插入子节点 * @param root * @param da…
[已知先序.中序求后序排列]--字符串类型 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和中序遍历的结果,还原这棵二叉树并输出其后序遍历的结果. 提示:分而治之--化大为小,化小为无 输入 每个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第一行为一个由大写英文字母组成的字符串,表示该二叉树的前序遍历的结果. 每组测试数据的第二行为一个由大写英文字母组成的字符串,表示该二叉树的…
//递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRecursion(root); System.out.println(); } //层次遍历 public void layerorder() { System.out.print("binaryTree层次遍历:"); LinkedList<Node<Integer>>…