迭代实现: 11/**2 2 * Definition for a binary tree node.3 3 * public class TreeNode {4 4 * public int val;5 5 * public TreeNode left;6 6 * public TreeNode right;7 7 * public TreeNode(int x) { val = x; }8 8 * }9 9 */1010publicclass Solution {
1111public IList<int> PreorderTraversal(TreeNode root) {
1212 List<int> result=new List<int>();
1313if (root==null)
1414return...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{class Program{publicclass nodes<T>{T data;nodes<T> Lnode, Rnode, Pnode;public T Data //中 {set { data = value; }get { return data; }}public nodes<T> LNode //左 {get { return Lnode; }set { Lnode = value; }}public nodes<T> RNode //右 {set { Rnode = va...
1. 引言在实际的项目中,树还是用的比较多的一种,尤其是对于具有层次结构的数据。相信很多人都学过树的遍历,比如先序遍历,后序遍历等,利用递归还是很容易理解的。今天给大家介绍下二叉树的几种遍历算法,包括递归和非递归的实现。首先建立一棵二叉树 如: [DebuggerDisplay("Value={Value}")]publicclass Tree{publicstring Value;public Tree Left;public Tree Right;}publicstatic Tree CreatFakeTree(){Tree tree = ...
树的定义树,由边连接的一些列节点。树是一种非线性的数据结构。根节点,树上最高的节点。父节点,某个节点的上层节点。子节点,某个节点的下层节点。叶子,没有任何子节点。二叉树二叉树,子节点的数量不超过两个的树。父节点的两个节点分别称为左节点和右节点。二叉查找树,是一种较小数据值存储在左节点,较大数据值存储在有节点的二叉树。遍历的概念 所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅...
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 快排
{class Tree{private int _data;private Tree _leftChild;private Tree _rightChild;public Tree(int value){this._data = value;this._leftChild = null;this._rightChild = null;}public int data{get { return this._data; }set { this._data = value; }}public Tree leftChild{get { return ...
前言
为什么会有线索化二叉树呢?
是这样子的,二叉树呢,比如有n个节点,那么就有n+1个空指针域。
这个是怎么来的呢?比如我们假如一个节点都有左子树和右子树,那么就有2n个节点。
但是我们发现连接我们节点的一共有n-1个(要把n个节点串起来),那么就等于2n-(n-1)=n+1。
那么如何利用这些空间?
假如中序遍历如下:
{8,3,10,1,6,14}
那么如果3的右节点为空,就指向它的后驱节点,也就是10。
如果3的左节点为空,那么就指向它的前驱...
题目:重建二叉树
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
解题思路
二叉树:二叉树是树的一种特殊结构,在二叉树中每个结点最多只能有两个子结点。在二叉树中最重要的操作是遍历,即按照某一顺序访问树中的所有结点。树的遍历方式:
前序遍历:...
题目内容给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。1/ \2 2/ \ / \
3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:1/ \2 2\ \3 3说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。实现方式一(递归实现)
/*** Definition for a binary tree node.* public class TreeNode {* public int val;* public TreeNode left;* public Tree...
我想像这样画出它:
我可以在控制台上绘制二叉树.我想用WPF绘制它.这是我为控制台编写的代码.class Program
{static void Main(string[] args){List<BinaryTreeData> myBinaryData = new List<BinaryTreeData>();myBinaryData.Add(new BinaryTreeData{ownID=1});myBinaryData.Add(new BinaryTreeData { parentID=1, ownID = 2 });myBinaryData.Add(new BinaryTreeData { parentID=1,ownID = 3 });foreach (var item in myBinaryDa...
104. Maximum Depth of Binary Tree
二叉树的最大深度
解题思路
深度优先搜索,将每一层的深度传给下一层,直到传到叶节点,将深度存入集合。最后取出集合中最大的数即为最大深度。/*** Definition for a binary tree node.* public class TreeNode {* public int val;* public TreeNode left;* public TreeNode right;* public TreeNode(int x) { val = x; }* }*/
public class Solution {public int MaxDepth(Tr...
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Write a data structure CBTInserter that is initialized with a complete binary tree and supports the following operations:CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root;
CBTInserter.insert(int v) will ...