dengxinyi 6 lat temu
rodzic
commit
2a24734cbe
1 zmienionych plików z 34 dodań i 0 usunięć
  1. 34 0
      medium/513.find-bottom-left-tree-value.go

+ 34 - 0
medium/513.find-bottom-left-tree-value.go

@@ -0,0 +1,34 @@
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func findBottomLeftValue(root *TreeNode) int {
+	if root == nil {
+		return -1
+	}
+	queue := make([]*TreeNode, 0)
+	queue = append(queue, root)
+	for len(queue) != 0 {
+		bl := queue[0].Val
+		nextLV := make([]*TreeNode, 0)
+		for len(queue) != 0 {
+			head := queue[0]
+			queue = queue[1:]
+			if head.Left != nil {
+				nextLV = append(nextLV, head.Left)
+			}
+			if head.Right != nil {
+				nextLV = append(nextLV, head.Right)
+			}
+		}
+		if len(nextLV) == 0 {
+			return bl
+		}
+		queue = nextLV
+	}
+	return -1
+}