Python solution beats 86.75% for Leetcode 863: All Nodes Distance K in Binary Tree

Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2Output: [7,4,1]Explanation: 
The nodes that are a distance 2 from the target node (with value 5)
have values 7, 4, and 1.
Note that the inputs "root" and "target" are actually TreeNodes.
The descriptions of the inputs above are just serializations of these objects.
  1. The given tree is non-empty.
  2. Each node in the tree has unique values 0 <= node.val <= 500.
  3. The target node is a node in the tree.
  4. 0 <= K <= 1000.
Debug window in VScode


  • 57/57 cases passed (32 ms)
  • Your runtime beats 86.75 % of python3 submissions
  • Your memory usage beats 62.5 % of python3 submissions (14.1 MB)




Data Science, Algorithm

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to find project ideas to practice web development(Not A Todo App)

CS 373 Spring 2021 Blog #7: Sriram Alagappan

What’s Cool in Azure — March 2020

10 Tools and Resources I Use for Web Development

Teach Your Kids to Code by Using the Turtle Library

What happens when you type in your browser and press Enter

Studies in mongoDB schema design — Pt 1

Logging in Kubernetes with Loki and the PLG Stack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Data Science, Algorithm

More from Medium

Heaps, the python way


Remove nth node from end of list Leetcode Python

LeetCode 15. 3Sum — Python Solution

Hackerrank — Insert Node in linked list walkthrough #Python #Hackerrank