标签: binary-tree

二叉树 hasPathSum() 实现

您好,我正在尝试实现hasPathSum() 给定数字的方法,根到叶节点之间是否存在任何路径。

我从斯坦福网站获得了这段代码。我认为这是错误的

/** 
 Given a tree and a sum, returns true if there is a path from the root 
 down to a leaf, such that adding up all the values along the path 
 equals the given sum. 
 Strategy: subtract the node value from the sum when recurring down, 
 and check to see if the sum is 0 when you run out of tree. 
*/ 

boolean hasPathSum(Node node, int sum) { 
  // return true if we run out of …
Run Code Online (Sandbox Code Playgroud)

java algorithm tree binary-tree data-structures

5
推荐指数
1
解决办法
6245
查看次数

从中序和层序遍历构造二叉树

首先,我想声明这不是家庭作业。我正在准备面试并遇到这个问题。我想我们可以通过中序级序遍历的定义。:-)。

例如:

      50
   /      \
 10        60
/  \       /  \
5   20    55    70
        /     /  \
      51     65    80
Run Code Online (Sandbox Code Playgroud)

上述树的中序和层序遍历为:

5、10、20、50、51、55、60、65、70、80

50, 10, 60, 5, 20, 55, 70, 51, 65, 80

我的点子:

(1) 遍历层序数组,找出出现在有序数组中的第一个元素。我们称这个元素为当前根。

(2) 在有序数组中找到当前根的索引。中序数组由索引分隔。中序数组的左边是当前根的左子树,中序数组的右边是当前根的右子树。

(3) 将有序数组更新为左边,然后转到步骤1。

(4) 将中序数组更新为其右侧,然后转到步骤2。

以上面的树为例。

(1) 5 is the first element appears in the in-order array. 

(2) [50 ...60] is the left sub-tree of 5 and [20 ... 80] is the right sub-tree of 5. 

(3) update the …
Run Code Online (Sandbox Code Playgroud)

binary-tree inorder data-structures

5
推荐指数
1
解决办法
4177
查看次数

概括二叉树遍历的动作?

我试图找到一种方法,我可以采用二叉树类并遍历其节点,在每个节点上
执行X数量的内联操作,而不必一遍又一遍地重写相同的遍历代码。
我想如果Java允许函数指针,这对我来说会更容易弄清楚......

基本上,我需要的是以下内容:

public class BinaryTreeNode {

    //...

    public void inOrderTraversalFrom(BinaryTreeNode node, /* ??? */ actions) {
        if(node.left != null)
            inOrderTraversalFrom(node.left);

        /* do something with "actions" here */

        if(node.right != null)
            inOrderTraversalFrom(node.right);
    }
}
Run Code Online (Sandbox Code Playgroud)


...其中动作可以允许执行不同的方法,根据要在每个节点上执行的动作类型采用不同的参数集

一个很好的例子是可以传递一个用于绘制这些节点的类,接受一个 Graphics 对象作为它的参数之一,而不是一个用于执行一些其他不需要 Graphics 的系列操作的类对象作为参数,而是一组完全不同的参数。

这怎么可能?执行此操作的最动态方式是什么?

java generics methods binary-tree function

5
推荐指数
1
解决办法
268
查看次数

使用旋转的二叉树变换

当我在学习关于二叉树的期中考试时,我发现了一个陈述,即任何任意的 n 节点二叉树都可以转换为最多 2*n-2 次旋转的任何其他 n 节点二叉树。有什么证据吗?我用渐近符号找到了某种证明,但不是那么清楚。我的意思是有人可以解释/说明为什么这是真的吗?如果它说 n 节点二叉树,它是否包括根?

algorithm math binary-tree data-structures tree-rotation

5
推荐指数
1
解决办法
4025
查看次数

BinaryTree (ctree, party) 的绘图忽略了 par() 的绘图选项

我想在图的上部绘制二叉树,并在第二部分(底部)制作第二个二叉树。下面是一些示例代码来显示,树的图完全忽略了由设置的分区选项par()

library("party")
### regression
airct <- ctree(Ozone ~ ., data = subset(airquality, !is.na(Ozone)))
### classification
irisct <- ctree(Species ~ .,data = iris)

par(mfrow = c(2, 1))
plot(airct)
plot(irisct)
Run Code Online (Sandbox Code Playgroud)

此代码不会在同一图(页面)中绘制两棵树。我该如何纠正?

即使遵循非常详细的答案在这种情况下也不起作用:由 'plot' 和 'ggplot' 并排生成的绘图 ctree 的绘图会忽略所有已建立的选项。

plot binary-tree r party par

5
推荐指数
1
解决办法
666
查看次数

关于完全二叉树的困惑

我一直看到它被定义为

完全二叉树是一种二叉树,其中每一层(可能除了最后一层)都被完全填充,并且所有节点都尽可能地向左。

但是..我不知道“所有节点都尽可能远离”是什么意思。这就是我的问题。我无法进一步扩展它,因为我不知道“所有节点都尽可能远离”是什么意思。比如..与什么相比尽可能地靠左?我不明白

binary-tree data-structures

5
推荐指数
1
解决办法
542
查看次数

PHP 中的广度优先搜索

我的表结构是:

id    name           parent   lft  rgt
1     abc              0       2    3
2     def              1       4    5
3     geh              1       6    7
4     ijk              2       8    9
5     lmn              2       10   11
Run Code Online (Sandbox Code Playgroud)

我正在做的是首先获取所有记录,然后使用深度优先搜索(DFS)为所有可能的孩子搜索树。

public function fetchRecursive($src_arr, $currentId, $parentFound = false)
{
    $cats = array();
    foreach ($src_arr as $row) {
        if ((!$parentFound && $row['id'] == $currentId) || $row['parent'] == $currentId) {
            $rowData = array();
            foreach ($row as $k => $v)
                $rowData[$k] = $v;
            $cats[] = $rowData;
            if ($row['parent'] == $currentId) {
                $cats …
Run Code Online (Sandbox Code Playgroud)

php tree binary-tree breadth-first-search

5
推荐指数
1
解决办法
3949
查看次数

克隆带有随机指针的二叉树

谁能解释用从左到右的随机指针克隆二叉树的方法?每个节点都有以下结构。

struct node {  
    int key; 
    struct node *left,*right,*random;
} 
Run Code Online (Sandbox Code Playgroud)

这是一个非常受欢迎的面试问题,我能够根据散列(类似于链表的克隆)找出解决方案。我试图理解链接(方法 2)中给出的解决方案,但也无法通过阅读代码弄清楚它想传达什么。我不期望基于散列的解决方案,因为它直观且非常直接。请解释基于修改二叉树并克隆它的解决方案。

c algorithm recursion binary-tree data-structures

5
推荐指数
1
解决办法
2577
查看次数

平衡与不平衡二叉树 - 需要澄清

我需要一些澄清,这可能是一个非常愚蠢的问题,我已经做了研究,但找不到我的问题的明确答案。我的问题是,平衡二叉树和不平衡二叉树之间有哪些属性差异?我在面试中被问到这个问题(java 问题),我已经向面试官解释了差异,但他提到他想知道区分两者的属性(二叉树 - 不平衡与平衡)。

如果有人可以为我澄清这一点,我将不胜感激。

java binary-tree

5
推荐指数
1
解决办法
2545
查看次数

使用 Javascript 的二叉树级顺序遍历

这是一个leetcode问题。

给定一棵二叉树,返回其节点值的层序遍历。(即从左到右,逐级)。

例如:给定二叉树[3, 9, 20, null, null, 15, 7]

    3
   / \
  9  20
    /  \
   15   7
Run Code Online (Sandbox Code Playgroud)

返回其层序遍历为:

[
  [3],
  [9,20],
  [15,7]
]
Run Code Online (Sandbox Code Playgroud)

但我正在 JavaScript 中尝试一种新的方式,而不是完全按照他们的解决方案。到目前为止,我能够打印数组,但是

如何在新行中打印不同的级别

以下是我到目前为止的代码:

var levelOrder = function(root) {
let output = [];
let queue = [];
let currentNode = root;
queue.push(currentNode);
let currentLevel = 1;
while(queue.length){
    
    currentNode = queue.shift();
    currentLevel--; //this will ensure we are adding new lines only on next level
    output.push(currentNode);
    
    if(currentNode.left){
        queue.push(currentNode.left);
    }
    if(currentNode.right){
        queue.push(currentNode.right);
    }
    
    if(currentLevel = 0){ …
Run Code Online (Sandbox Code Playgroud)

javascript binary-tree breadth-first-search multidimensional-array

5
推荐指数
1
解决办法
1711
查看次数