LeetCode 力扣官方题解 | 513. 找树左下角的值

LeetCode 力扣官方题解 | 513. 找树左下角的值

题号:513. 找树左下角的值

题目描述
难易度:中等
给定一个二叉树的 根节点 root ,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

示例 1:
在这里插入图片描述

输入: root = [2,1,3]
输出: 1

示例 2:
在这里插入图片描述

输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7

解决方案

方法一:深度优先搜索
使用 height 记录遍历到的节点的高度,curVal 记录高度在 curHeight 的最左节点的值。
在深度优先搜索时,我们先搜索当前节点的左子节点,再搜索当前节点的右子节点,然后判断当前节点的高度 height 是否大于 curHeight ,如果是,那么将 curVal 设置为当前结点的值,curHeight 设置为 height 。
因为我们先遍历左子树,然后再遍历右子树,所以对同一高度的所有节点,最左节点肯定是最先被遍历到的。

代码

Python3

class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        curVal = curHeight = 0
        def dfs(node: Optional[TreeNode], height: int) -> None:
            if node is None:
                return
            height += 1
            dfs(node.left, height)
            dfs(node.right, height)
            nonlocal curVal, curHeight
            if height > curHeight:
                curHeight = height
                curVal = node.val
        dfs(root, 0)
        return curVal

C++

class Solution {
public:
    void dfs(TreeNode *root, int height, int &curVal, int &curHeight) {
        if (root == nullptr) {
            return;
        }
        height++;
        dfs(root->left, height, curVal, curHeight);
        dfs(root->right, height, curVal, curHeight);
        if (height > curHeight) {
            curHeight = height;
            curVal = root->val;
        }
    }

    int findBottomLeftValue(TreeNode* root) {
        int curVal, curHeight = 0;
        dfs(root, 0, curVal, curHeight);
        return curVal;
    }
};

Java

class Solution {
    int curVal = 0;
    int curHeight = 0;

    public int findBottomLeftValue(TreeNode root) {
        int curHeight = 0;
        dfs(root, 0);
        return curVal;
    }

    public void dfs(TreeNode root, int height) {
        if (root == null) {
            return;
        }
        height++;
        dfs(root.left, height);
        dfs(root.right, height);
        if (height > curHeight) {
            curHeight = height;
            curVal = root.val;
        }
    }
}

C#

public class Solution {
    int curVal = 0;
    int curHeight = 0;

    public int FindBottomLeftValue(TreeNode root) {
        int curHeight = 0;
        DFS(root, 0);
        return curVal;
    }

    public void DFS(TreeNode root, int height) {
        if (root == null) {
            return;
        }
        height++;
        DFS(root.left, height);
        DFS(root.right, height);
        if (height > curHeight) {
            curHeight = height;
            curVal = root.val;
        }
    }
}

C

void dfs(const struct TreeNode *root, int height, int *curVal, int *curHeight) {
    if (root == NULL) {
        return;
    }
    height++;
    dfs(root->left, height, curVal, curHeight);
    dfs(root->right, height, curVal, curHeight);
    if (height > *curHeight) {
        *curHeight = height;
        *curVal = root->val;
    }
}

int findBottomLeftValue(struct TreeNode* root){
    int curVal, curHeight = 0;
    dfs(root, 0, &curVal, &curHeight);
    return curVal;
}

Golang

func findBottomLeftValue(root *TreeNode) (curVal int) {
    curHeight := 0
    var dfs func(*TreeNode, int)
    dfs = func(node *TreeNode, height int) {
        if node == nil {
            return
        }
        height++
        dfs(node.Left, height)
        dfs(node.Right, height)
        if height > curHeight {
            curHeight = height
            curVal = node.Val
        }
    }
    dfs(root, 0)
    return
}

JavaScript

var findBottomLeftValue = function(root) {
    const dfs = (root, height) => {
        if (!root) {
            return;
        }
        height++;
        dfs(root.left, height);
        dfs(root.right, height);
        if (height > curHeight) {
            curHeight = height;
            curVal = root.val;
        }
    }

    let curHeight = 0;
    dfs(root, 0);
    return curVal;
};

复杂度分析

时间复杂度:O(n) ,其中 n 是二叉树的节点树目。需要遍历 n 个节点。
空间复杂度:O(n) ,递归栈需要占用 O(n) 的空间。

方法二:广度优先搜索
思路

使用广度优先搜索遍历每一层的节点。在遍历一个节点时,需要先把它的非空右子节点放入队列,然后再把它的非空左子节点放入队列,这样才能保证从右到左遍历每一层的节点。广度优先搜索所遍历的最后一个节点的值就是最底层最左边节点的值。

代码

Python3

class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        q = deque([root])
        while q:
            node = q.popleft()
            if node.right:
                q.append(node.right)
            if node.left:
                q.append(node.left)
            ans = node.val
        return ans

Java

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int ret = 0;
        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode p = queue.poll();
            if (p.right != null) {
                queue.offer(p.right);
            }
            if (p.left != null) {
                queue.offer(p.left);
            }
            ret = p.val;
        }
        return ret;
    }
}

C#


public class Solution {
    public int FindBottomLeftValue(TreeNode root) {
        int ret = 0;
        Queue<TreeNode> queue = new Queue<TreeNode>();
        queue.Enqueue(root);
        while (queue.Count > 0) {
            TreeNode p = queue.Dequeue();
            if (p.right != null) {
                queue.Enqueue(p.right);
            }
            if (p.left != null) {
                queue.Enqueue(p.left);
            }
            ret = p.val;
        }
        return ret;
    }
}

C++


class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        int ret;
        queue<TreeNode *> q;
        q.push(root);
        while (!q.empty()) {
            auto p = q.front();
            q.pop();
            if (p->right) {
                q.push(p->right);
            }
            if (p->left) {
                q.push(p->left);
            }
            ret = p->val;
        }
        return ret;
    }
};

C


#define MAX_NODE_SIZE 10000

int findBottomLeftValue(struct TreeNode* root){
    int ret;
    struct TreeNode** queue = (struct TreeNode **)malloc(sizeof(struct TreeNode) * MAX_NODE_SIZE);
    int head = 0;
    int tail = 0;
    queue[tail++] = root;
    while (head != tail) {
        struct TreeNode *p = queue[head++];
        if (p->right) {
            queue[tail++] = p->right;
        }
        if (p->left) {
            queue[tail++] = p->left;
        }
        ret = p->val;
    }
    free(queue);
    return ret;
}

Golang

func findBottomLeftValue(root *TreeNode) (ans int) {
    q := []*TreeNode{root}
    for len(q) > 0 {
        node := q[0]
        q = q[1:]
        if node.Right != nil {
            q = append(q, node.Right)
        }
        if node.Left != nil {
            q = append(q, node.Left)
        }
        ans = node.Val
    }
    return
}

JavaScript

var findBottomLeftValue = function(root) {
    let ret = 0;
    const queue = [root];
    while (queue.length) {
        const p = queue.shift();
        if (p.right) {
            queue.push(p.right);
        }
        if (p.left) {
            queue.push(p.left);
        }
        ret = p.val;
    }
    return ret;
};

复杂度分析
时间复杂度:O(n) ,其中 n 是二叉树的节点数目。
空间复杂度:O(n) ,如果二叉树是满完全的二叉树,那么队列 q 最多保存「n /2」个节点。

详情请点:
LeetCode 力扣官方题解 | 513. 找树左下角的值

编辑&版式:pingping

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值