본문 바로가기
728x90
반응형

Data structure7

Sum of nodes at maximum depth of a Binary Tree Recursive: maxDepthSum 함수 : 각 depth에 있는 node들의 합을 구하는 함수 maxDepth 함수에서 while 루프를 통해 최대 depth를 구한다. leaf node의 합을 구하고 싶으므로 , depth-1 을 사용 #include struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; int maxDepthSum(TreeNode* root, int depth) { if (root == nullptr) { return 0; } if (depth == 0) { return root->val; } int leftSum .. 2023. 10. 4.
Find the Maximum Depth or Height of a Tree using Level Order Traversal Do Level Order Traversal, while adding Nodes at each level to Queue, we have to add NULL Node so that whenever it is encountered, we can increment the value of variable and that level get counted. "Level Order Traversal"을 사용하여 트리의 최대 깊이 또는 높이를 찾는 문제는 주어진 트리의 레벨 순서로 노드를 탐색하여 트리의 최대 깊이 또는 높이를 찾는 문제입니다. 일반적으로 이 문제를 해결하기 위해 큐(Queue) 자료구조를 사용하여 너비 우선 탐색(Breadth-First Search, BFS)을 수행합니다. BFS를 사용하면 트리.. 2023. 10. 3.
Find the Maximum Depth or Height of given Binary Tree Given a binary tree, the task is to find the height of the tree. The height of the tree is the number of vertices in the tree from the root to the deepest node. Note: The height of an empty tree is 0 and the height of a tree with single node is 1. Example of Binary Tree Recursively calculate the height of the left and the right subtrees of a node and assign height to the node as max of the heigh.. 2023. 10. 3.
Linked List(Check if Linked List is Palindrome) Linked List is a data structure comprising a sequence of nodes, each containing a reference to the subsequent node in the sequence. Linked lists are vital for dynamic memory allocation and offer primary operations such as insertion, deletion, and traversal. They come in handy when working with variable data sizes, enabling the efficient management of data storage and retrieval. Representation of.. 2023. 10. 3.
Queues Queues are containers of objects based on arrays that follow a First-In-First-Out (FIFO) order, meaning that the least recently added item is removed first. Queues are helpful in cases where the FIFO order of data needs to be maintained, such as for caching and handling real-time system interruptions. A real-life example of a queue data structure is a line at a ticket counter or amusement park r.. 2023. 10. 3.
Stacks Stacks are containers of objects based on arrays that follow a Last-In-First-Out (LIFO) order, meaning that the most recently added item is removed first. Stacks are often used with the recursive backtracking algorithm and as the mechanism supporting the “undo” or “back” button in applications and browsers. Compilers often use stacks to parse the syntax of expressions before translating them int.. 2023. 10. 3.
Arrays An array is a sequential list of values. You can use arrays to store and access multiple values of the same data type under one identifier. Arrays makes it easier to perform operations on an entire dataset. They’re also great for retrieving data. There are one-dimensional arrays and multi-dimensional arrays. The values stored in an array are called elements. Elements are stored in consecutive bl.. 2023. 10. 3.
728x90
반응형